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

        //list 为全量集合
        int batchCount = 1500; //每批插入数目
        int batchLastIndex = batchCount;
        List> 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 subList : shareList) {
                //循环插入数据
                zcAssetsCheckDetailDao.insertBatch(subList);
            }
        }

另附mybatis Oracle批量插入语法


     insert into tables (col1,col2,col3)
     select A.* from 
    
    select 
    #{item.col1} as col1,
    #{item.col2} as col2,
    #{item.col3} as col3
    from dual
  
  A

你可能感兴趣的:(【java】分批插入数据库拆分list算法)