MGB可以反向生成对应数据表的实体bean、mapper接口类以及对应的sqlmap.xml,关于该插件的详细介绍可以参考官方介绍。
接下来整理一下使用maven管理的SSM框架中如何使用mybaties-generator插件:
1、pom中引入插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
2、配置generatorConfiguration:在resources目录下新建一个xml配置文件,自行命名,主要配置信息如下
<classPathEntry>配置链接数据库的jar路径,本项目路径为maven仓库下的jar路径
<context>配置数据库,有多个数据库可配置多个context
<commentGenerator> suppressAllComments 配置为true时去掉注释,默认是false,会生成大量的注释信息
<table>要反向生成的表,定义库、表名、实体bean名称,*ByExample默认为true,建议配置为false,为true会生成很多无用的sql,有兴趣的可以试下
<?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>
<classPathEntry
location="D:\tools\maven\repository\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" />
<context id="test" targetRuntime="MyBatis3">
<!-- 注释处理 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- jdbc -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/test"
userId="root" password="" />
<!-- bean -->
<javaModelGenerator targetPackage="com.dubbo.biz"
targetProject="D:\project\dubbo-consumer\biz\src\main\java" />
<!-- mapper.xml -->
<sqlMapGenerator targetPackage="com.dubbo.mapper"
targetProject="D:\project\dubbo-consumer\dao\src\main\resources" />
<!-- dao -->
<javaClientGenerator targetPackage="com.dubbo.dao"
targetProject="D:\project\dubbo-consumer\dao\src\main\java" type="XMLMAPPER" />
<!-- table -->
<table schema="test" tableName="temp_file_info"
domainObjectName="TempFileInfo" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
<table schema="test" tableName="temp_log"
domainObjectName="TempLog" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
3、配置文件声明好后,执行生成文件:
在插件处直接右键选择Run Maven Build,或者选择该配置文件--> Run Maven --> New Goal --> 输入:mybaties-generator:generate
4、执行以上步骤后即会生成相应的文件,修改之后重新执行即可(会覆盖相同文件,但是已生成的文件不会自动删除)