Java 中List分批插入数据库

直接贴代码,smsNameLists为需要拆分的集合

	//每批插入数目
	int batchCount = 1000;
    int batchLastIndex = batchCount;
    List<List<SmsNameList>> shareList = new ArrayList<>();
    for (int index = 0; index < smsNameLists.size(); ) {
    	if (batchLastIndex >= smsNameLists.size()) {
        	batchLastIndex = smsNameLists.size();
            shareList.add(smsNameLists.subList(index, batchLastIndex));
            break;
       	} else {
           	shareList.add(smsNameLists.subList(index, batchLastIndex));
            // 设置下一批下标
            index = batchLastIndex;
            batchLastIndex = index + (batchCount - 1);
        }
    }
    if (CollectionUtils.isNotEmpty(shareList)) {
        for (List<SmsNameList> subList : shareList) {
           	//循环插入数据
            saveBatch(subList);
        }
    }

你可能感兴趣的:(集合)