数据库逆向生成代码

PPT

This article will mainly teach you how to use MyBatis Generator(MBG) to
generate SQL automatically for simple CRUD functions on each table in a
configuration with IDEA.

数据库逆向生成代码_腾讯视频

This article covers###

1.背景介绍

1. Introduction
2. Generated Files
3. Steps

做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类,这是理所当然的。但是一般公司里做项目时,没有说既进行完数据库设计后还要再“自己”建立一遍实体类。意思是,在项目设计时,要么根据需求分析建立实体类,由正向生成数据库表;要么就先进行数据库表设计,再逆向生成实体类。没有说进行完任意一方的设计后再去花时间去自己匹配建立另一方的设计。

If you just want a Quick Start, to ignore the Introduction and
Generated Files is fine, jump to the third part Steps.

原因:

1. Introduction

Mybatis Generator is a code generator for MyBatis and iBATIS. It
generate code for all versions of MyBatis, and versions of iBATIS after
version 2.2.0. Using MBG can reduce our workload and also reduce many
stupid errors, maybe you just hand by mistake.


1、2倍工作量,浪费时间。(时间对公司来说很重要)

2. Generated Files##

2.1 POJOs
2.2 XML Files
2.3 Java client classes

2、无法保证两边映射一致。因为两边都是自己设计的,那么就需要人为保证映射关系统一,会有隐患。

2.1 POJOs###

  • MBG will automatically generate Java POJOs(Plain Old Java Objects)
    that match the structure. This may include:

    • a class to match the primary key of the table(if there is a
      primary key)
    • a class to match the non-primary key fields of the table(except
      BLOB fields)
    • a class to include the BLOB fields of a table(if the table has
      BLOB fields)

3、开发、维护上都不利。因为如果任意一方因某些原因需要改用,那么经常要两边都要改,这样的话映射不一致的可能性不仅更大,而且效率低下。而如果利用正逆向工程的话,则很容易解决。

2.2 XML Files###

  • MBG will generates SQL for simple CRUD functions on each table in a
    configuration. The generated SQL statements include:

    • insert
    • update by primary key
    • update by example(using a dynamic where clause)
    • delete by primary key
    • delete by example(using a dynamic where clause)
    • select by primary key
    • select by example(using a dynamic where clause)
    • count by example

Also,according to your table, MBG will generate SQL statements
flexibilly. For example, without a primary key in your table, MBG will
not generate a selectByPrimary function.
  What’s more, MBG will merge xml files, that is to say, if you have
already created your xml files with same names, these files will not be
overwrite or replace, all of the SQL statements will be pull in the same
file.


2、知识剖析

2.3 Java client classes###

  • MBG will generate java clients for mybatis3 as the following type:
    • mapper interfaces, for example XxxMapper.java, being differenced
      from ibatis 2.x, it can be used as DAOs in your project in
      direct.
  • MBG will also generate the following types java clients for ibatis
    2.x:

    • DAOs that conform to the spring framework
    • DAOs that only use the iBATIS SQL mapping API. These DAOs can be
      generated in two varieties: supplying the SqlMapClient through
      either constructor or setter injection.
    • DAOs that conform to the iBATIS DAO Framework (an optional part
      of iBATIS, this framework is now deprecated and we suggest that
      you use the Spring framework instead).

MBG will not merge those same-name java files, which could be
overwrite, or the newly generated file would be saved as a unique name,
then you have to merge by hand. But both IDEA and MyEclipse will do you
a favor, it will merge those file for you .


MyBatis Generator
是一个专门为Mybatis和iBATIS写的代码生成器。它可以为所有版本的Mybatis和2.2.0之后版本的iBATIS生成代码。MBG可以生成一些简单的CRUD的数据库操作,但稍微复杂的sql仍需要自己写。

3. Steps##

  • #### Step 1####

In order to run MBG, you need to prepare a generatorConfig.xml. At
a minimun, the following element should be specified:

  • <jdbcConnection>永利澳门游戏网站,:With
    driverClass、connectionURL、userId、password, MBG can connect the
    target database.
  • <javaModelGenerator>:With targetPackage、targetProject, MBG
    can specify target package and project for generated java models.
  • <sqlMapGenerator>:With targetPackage、targetProject, MBG can
    specify target package and project for generated SQL Map file.
  • <javaClientGenerator>:(Optional):With type, targetPackage
    and targetProject.
  • <table>: to specify tableName,
    domainObjectName,enableCountByExample…
  • #### Step 2####

Run MBG command line like this:

 java -jar mybatis-generator-core-x.x.x.jar -configfile tempgeneratorConfig.xml -overwrite

In IDEA, you can just click the mybatis-generator plugin. Then MBG
will automatically generate those file for you .

永利澳门游戏网站 1

mybatis-generator.png

Noted:Though MBG generate these code for you, it doesn’t mean that you
can use them directly in you project, for mybatis, it still requires a
MapperConfig.xml file.
  For more xml configuration reference, see as XML Configuration File
Reference.

Here is my code resources:
链接:http://pan.baidu.com/s/1i4IOz57
密码:gjex

Pojo类

匹配的主键的类,匹配非主键字段的类,匹配BLOB字段的类,用来处理动态SELECT,DELETE,UPDATE的类。

XML文件

insert

update by primary key

发表评论

电子邮件地址不会被公开。 必填项已用*标注