难题解决:Mycat数据库中间件+Mybatis批量插入数据并返回行记录的所有主键ID

 一、mybatis的版本必须为3.3.1及其以上

项目所依赖的mybatis的版本必须为3.3.1及其以上,低版本的不行,保证hap项目的依赖的mybatis的jar的版本必需为需要的版本:

 

二、在Dao层不能使用@Param注解,且Mapper.xml文件中使用list变量接收Dao层中的集合

 数据库库结构设计:

对应的Dto层:

对应的Mapper接口:

对应的Mapper.xml文件:

具体代码如下,需要使用到mycat的注解,指定该插入为批量数据插入:

<insert id="insertByBatch">
/*!mycat:catlet=io.mycat.route.sequence.BatchInsertSequence */
insert into test_sharding_by_accounting_date (rule_code,name,accounting_date)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.ruleCode,jdbcType=DECIMAL},#{item.name,jdbcType=VARCHAR},#{item.accountingDate,jdbcType=DATE})
</foreach>
</insert>

三、Mycat为分库表做全局序列的配置

schema.xml文件:

server.xml文件的全局序列方式设置为本地读取:

 sequence_conf.properties文件的配置:

原文地址:https://www.cnblogs.com/jiaqingshareing/p/9278358.html