美文网首页
【java】分批插入数据库拆分list算法

【java】分批插入数据库拆分list算法

作者: 棒棒糖RV | 来源:发表于2018-06-01 09:04 被阅读0次
        //list 为全量集合
        int batchCount = 1500; //每批插入数目
        int batchLastIndex = batchCount;
        List<List<ZcAssetsCheckDetail>> shareList = new ArrayList<>();
        for (int index = 0; index < list.size(); ) {
            if (batchLastIndex >= list.size()) {
                batchLastIndex = list.size();
                shareList.add(list.subList(index, batchLastIndex));
                break;
            } else {
                shareList.add(list.subList(index, batchLastIndex));
                index = batchLastIndex;// 设置下一批下标
                batchLastIndex = index + (batchCount - 1);
            }
        }

        if (CollectionUtils.isNotEmpty(shareList)) {
            for (List<ZcAssetsCheckDetail> subList : shareList) {
                //循环插入数据
                zcAssetsCheckDetailDao.insertBatch(subList);
            }
        }

另附mybatis Oracle批量插入语法

<insert id="insertBatch" parameterType="java.util.List">
     insert into tables (col1,col2,col3)
     select A.* from 
    <foreach open="(" close=")" collection="list" item="item" index="index" separator="UNION ALL">
    select 
    #{item.col1} as col1,
    #{item.col2} as col2,
    #{item.col3} as col3
    from dual
  </foreach>
  A
</insert>

相关文章

网友评论

      本文标题:【java】分批插入数据库拆分list算法

      本文链接:https://www.haomeiwen.com/subject/atdhsftx.html