mybatis generator一对一映射,一对多映射,批量插入,批量更新

mybatis-generator这是原来的github地址 
现在在此基础上添加了一对一和一对多映射配置,配置在table标签上 
如果需要一对一配置生成,需要添加插件配置 
在context中添加如下配置

<!-- 生成一对一配置 -->
<plugin type="cc.bandaotixi.plugins.OneToOnePlugin"></plugin>
  • 1
  • 2

一对多配置如下

<!-- 生成一对多配置 -->
<plugin type="cc.bandaotixi.plugins.OneToManyPlugin"></plugin>
  • 1
  • 2

批量插入和批量更新

<plugin type="cc.bandaotixi.plugins.BatchInsertPlugin"></plugin>
<plugin type="cc.bandaotixi.plugins.BatchUpdatePlugin"></plugin>
  • 1
  • 2

如果需要把数据库注释添加到java文件中,配置如下

<commentGenerator type="cc.bandaotixi.plugins.BDTCommentGenerator">
    <property name="javaFileEncoding" value="UTF-8"/>
    <property name="suppressDate" value="true"/>
    <property name="suppressAllComments" value="false" /></commentGenerator>
  • 1
  • 2
  • 3
  • 4

那么配置一对一表时应这么配置

<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>
  • 1
  • 2
  • 3
  • 4

配置一对多表时

<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>
  • 1
  • 2
  • 3
  • 4

下面补上全部配置

<?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&amp;characterEncoding=utf-8&amp;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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

值得注意的是,如果使用批量更新功能需要在连接的配置上添加allowMultiQueries=true 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值