您的位置:首页 > 其它

myBatis逆向工程知多少

2017-06-24 12:29 357 查看
myBatis逆向工程知多少


mybatis逆向工程是什么?

       在弄明白什么是逆向工程前,我们是不是要明白什么是“正向工程”,
正向工程就是:用代码-->生成数据表,具体是例子请参考:Hibernate写实体映射方法


 

     逆向工程定义:mybatis官方为了提高开发的效率,提高自动对单表生成sql,包括:mapper.xml、mapper.java,表名.java(PO类),在企业开发中同城是设计阶段对表设计、创建,在开发阶段更加表结构创建对应的PO类。

MyBatis Generator (MBG):
       which is a code generator for MyBatis MyBatis and iBATIS. It will generate code for all versions of MyBatis, and versions of iBATIS after version 2.2.0. It will introspect a database table (or many tables) and will generate
artifacts that can be used to access the table(s). This lessens the initial nuisance of setting up objects and configuration files to interact with database tables. MBG seeks to make a major impact on the large percentage of database operations that are simple
CRUD (Create, Retrieve, Update, Delete). You will still need to hand code SQL and objects for join queries, or stored procedures.


2,有什么好处?

      Mybatis逆向工程方向:数据库表----java代码(只要有了表,后面的实体POJO,dao层都可以通过mybatis逆向工程自动生成)。

本博文介绍是直接通过插件mybatis-generator-core执行生成mapper和pojo包的。

   插件下载地址:http://pan.baidu.com/s/1eSH0sMI
 



3,怎么逆向工程?

 


下面是运用插件逆向生成mapper和pojo:

1,在开发工具(以Eclipse为例)中导入插件:


 

如下图所示:
 


 

2,配置generatorConfig.xml文件:

      在generatorConfig.xml中进行数据库的配置,生命PO类的位置,生命mapper的位置,填写需要逆向那些表等等。
  配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.16822.226:3306/taotao" userId="root"
password="root">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.taotao.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.taotao.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.taotao.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_content" />
<table schema="" tableName="tb_content_category" />
<table schema="" tableName="tb_item" />
<table schema="" tableName="tb_item_cat" />
<table schema="" tableName="tb_item_desc" />
<table schema="" tableName="tb_item_param" />
<table schema="" tableName="tb_item_param_item" />
<table schema="" tableName="tb_order" />
<table schema="" tableName="tb_order_item" />
<table schema="" tableName="tb_order_shipping" />
<table schema="" tableName="tb_user" />
</context>
</generatorConfiguration>


3,执行逆向工程操作:   

 


 

4,生成mapper和pojo包:

      之后生成mapper和Pojo包,分别放入到dao和pojo文件夹中。

 


 

将整两个文件复制到dao和pojo文件中如下所示:

 


 

参考文档:

     (1)官方文档:  http://www.mybatis.org/generator/configreference/xmlconfig.html
          

     (2)Mybatis的逆向工程:http://blog.csdn.net/u014010769/article/details/47347775
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: