mybatis+oracle的批量插入
作者:互联网
// 批量插入,手动控制事务
SqlSession batchSqlSession = null;
try {
batchSqlSession = sqlSessionTemplate.getSqlSessionFactory()
.openSession(ExecutorType.BATCH, false);// 获取批量方式的sqlsession
int batchCount = 1000;// 每批commit的个数
int batchLastIndex = batchCount - 1;// 每批最后一个的下标
for (int index = 0; index < saveList.size();) {
if (batchLastIndex > saveList.size()) {
batchLastIndex = saveList.size();
batchSqlSession.insert(
"com.jomoo.oms.baseData.mapper.CapaCityMapper.create2",
saveList.subList(index, batchLastIndex));
batchSqlSession.commit();
break;// 数据插入完毕,退出循环
} else {
batchSqlSession.insert(
"com.jomoo.oms.baseData.mapper.CapaCityMapper.create2",
saveList.subList(index, batchLastIndex));
batchSqlSession.commit();
index = batchLastIndex;// 设置下一批下标
batchLastIndex = index + (batchCount - 1);
}
}
} catch (Exception e) {
throw new OptException(e.getMessage());
} finally {
batchSqlSession.close();
}
<insert id="create2" parameterType="com.jomoo.oms.baseData.model.CapaCityModel">
<selectKey keyProperty="id" order="BEFORE" resultType="Long">
select JOMOO_OMS.OMS_CAPACITY_S.NEXTVAL as id from dual
</selectKey>
insert into JOMOO_OMS.OMS_CAPACITY(<include refid="Base_Column_List"></include>) select JOMOO_OMS.OMS_CAPACITY_S.NEXTVAL, A.*
from (
<foreach collection="list" item="capacity" index="index" open="" close="" separator="union all">
select
#{capacity.factoryCode},
#{capacity.factoryName},
#{capacity.productSeriesCode},
#{capacity.productSeries},
#{capacity.materialCode},
#{capacity.materialName},
#{capacity.dateNumber},
#{capacity.totalCapacity},
#{capacity.leftCapacity},
#{capacity.remark},
#{capacity.registerId},
#{capacity.createdByEmployee},
#{capacity.createdByDept},
#{capacity.enableFlag},
sysdate
from dual
</foreach>
) A
</insert>
标签:index,capacity,批量,saveList,batchSqlSession,OMS,oracle,batchLastIndex,mybatis 来源: https://www.cnblogs.com/mabaoqing/p/10691526.html