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

        //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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容