java小技巧:如何分批次导入大量数据
2019-11-28 本文已影响0人
深夜小码农
//List 需要导入的数据
int count = 1000;//每批次导入的数目
int Lastindex = count;
List<List<T>> shareList = new ArrayList<>();
for(int index = 0;index < List.size()){
if(Lastindex >= List.size){
Lastindex = List.size();
shareList.add(List.subList(index,Lastindex));
break;
} else {
shareList.add(List.subList(index,Lastindex));
index = Lastindex ;//设置下一次标记
Lastindex = index + (count - 1);
}
}
if(CollectionUtils.isNoEmpty(shareList)){
for(List<T> subList : shareList){
TMapper.insert(subList);
}
}
}