最近一个老项目集成mybatis,利用 generator自动生成实体类、mapper的时候折腾了一小时,记录一下,避免以后再折腾
很简单的三步 https://gitee.com/shunangua/mybatisgenerator.git
1、pom 添加依赖,配置插件
2、在maven项目下的src/main/resources/mybatis 目录下建立名为 generatorConfig.xml的配置文件,作为mybatis-generator-maven-plugin 插件的执行目标,模板如下:
<?xml version="1.0" encoding="UTF-8"?><context id="mu_mysql_tables" targetRuntime="MyBatis3">
<!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:Mysql://localhost:3306/pay?characterEncoding=UTF-8"
userId="root"
password="123456" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 数据表对应的model层 -->
<javaModelGenerator targetPackage="com.lianzi.wx.pay.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- sql mapper 映射配置文件 -->
<sqlMapGenerator targetPackage="com.lianzi.wx.pay.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- mybatis3中的mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lianzi.wx.pay.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 这里一定要配置,数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO -->
<table schema="pay" tableName="order_form" domainObjectName="OrderForm"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
3、使用maven运行mybatis-generator-maven-plugin插件:工程名->Plugins->mybatis-generator->mybatis-generator:generate->Run Maven Build,自动生成文件