mybatis-generator这是原来的github地址
现在在此基础上添加了一对一和一对多映射配置,配置在table标签上
如果需要一对一配置生成,需要添加插件配置
在context中添加如下配置
<!-- 生成一对一配置 -->
<plugin type="cc.bandaotixi.plugins.OneToOnePlugin"></plugin>
一对多配置如下
<!-- 生成一对多配置 -->
<plugin type="cc.bandaotixi.plugins.OneToManyPlugin"></plugin>
批量插入和批量更新
<plugin type="cc.bandaotixi.plugins.BatchInsertPlugin"></plugin>
<plugin type="cc.bandaotixi.plugins.BatchUpdatePlugin"></plugin>
如果需要把数据库注释添加到java文件中,配置如下
<commentGenerator type="cc.bandaotixi.plugins.BDTCommentGenerator">
<property name="javaFileEncoding" value="UTF-8"/>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="false" /></commentGenerator>
那么配置一对一表时应这么配置
<table tableName="t_test" domainObjectName="TEst">
<generatedKey column="test_id" sqlStatement="MySql" identity="true" />
<oneToOne mappingTable="t_test" column="test_id" joinColumn="parent_test_id" where="t_test.is_deleted=0" />
</table>
配置一对多表时
<table tableName="t_test" domainObjectName="Test">
<generatedKey column="test_id" sqlStatement="MySql" identity="true" />
<oneToMany mappingTable="t_test" column="test_id" joinColumn="parent_test_id" where="t_test.is_deleted=0" />
</table>
下面补上全部配置
<?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">
<!--
生成mapper命令
java -jar mybatis.jar -configfile generatorConfig.xml -overwrite
-->
<generatorConfiguration>
<classPathEntry location="C:\Users\administrator\.m2\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar" />
<context id="appInfo" targetRuntime="MyBatis3">
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 生成一对一配置 -->
<plugin type="cc.bandaotixi.plugins.OneToOnePlugin"></plugin>
<!-- 生成一对多配置 -->
<plugin type="cc.bandaotixi.plugins.OneToManyPlugin"></plugin>
<plugin type="cc.bandaotixi.plugins.BatchInsertPlugin"></plugin>
<plugin type="cc.bandaotixi.plugins.BatchUpdatePlugin"></plugin>
<commentGenerator type="cc.bandaotixi.plugins.BDTCommentGenerator">
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="false" />
</commentGenerator>
<jdbcConnection connectionURL="jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf-8&useSSL=true" driverClass="com.mysql.jdbc.Driver" password="test" userId="root" />
<!-- 数据表对应的实体层 -->
<javaModelGenerator targetPackage="com.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
<property name="isMergeable" value="false"/>
</sqlMapGenerator>
<!-- 在ibatis2 中是dao层,但在mybatis3中,其实就是mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="t_test" domainObjectName="Test">
<generatedKey column="test_id" sqlStatement="MySql" identity="true" />
<oneToMany mappingTable="t_test" column="parent_est_id" joinColumn="test_id" where="t_test.is_deleted=0" />
<!--映射的表必须配置table-->
</table>
</context>
</generatorConfiguration>
值得注意的是,如果使用批量更新功能需要在连接的配置上添加allowMultiQueries=true
如果你比较懒,不想编译代码也可以直接下载,但是需要一个积分
如果需要进一步添加插件,请fork郗瑞强MybatisGenerator工具
如果有问题或者BUG请发邮件到:bandaotixi@hotmail.com
感谢
很抱歉,因为代码很久远,这里已经不再维护,请理解