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

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&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>

值得注意的是,如果使用批量更新功能需要在连接的配置上添加allowMultiQueries=true
如果你比较懒,不想编译代码也可以直接下载,但是需要一个积分
如果需要进一步添加插件,请fork郗瑞强MybatisGenerator工具
如果有问题或者BUG请发邮件到:bandaotixi@hotmail.com
感谢

很抱歉,因为代码很久远,这里已经不再维护,请理解

MyBatis Generator 配置文件中添加批量插入需要进行以下步骤: 1. 添加插件 在 `<context>` 中添加以下插件: ```xml <plugins> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" /> <plugin type="org.mybatis.generator.plugins.BatchInsertPlugin"> <property name="batchSize" value="100" /> </plugin> </plugins> ``` - `UnmergeableXmlMappersPlugin` 插件用于禁止合并 XML 映射文件,可以避免生成的 SQL 语句不符合预期。 - `RowBoundsPlugin` 插件用于支持分页查询。 - `BatchInsertPlugin` 插件用于添加批量插入功能。`batchSize` 属性指定每次批量操作的数据条数。 2. 配置表信息 在 `<table>` 中添加以下属性: ```xml <table tableName="your_table_name" domainObjectName="YourTable" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" enableInsert="true" enableSelective="true" /> ``` - `enableInsert="true"` 表示启用插入操作。 - `enableSelective="true"` 表示启用插入操作的可选属性。 3. 生成代码 执行 MyBatis Generator 的命令,生成代码。生成的代码中会包含批量插入的 SQL 语句。 4. 使用批量插入 在代码中使用批量插入时,需要调用 `insertList` 方法,将数据列表作为参数传入: ```java List<YourTable> list = new ArrayList<YourTable>(); // 添加数据到 list yourTableMapper.insertList(list); ``` 以上就是在 MyBatis Generator 配置文件中添加批量插入的步骤。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值