在使用saveAll()等方法時,為了防止集合元素過大,使用splitList將原集合,分割成若干個小集合
import java.util.ArrayList;
import java.util.List;public class ListUtils {/*** 將集合拆分成若干個batch,并將batch存于新的集合** @param list 目標集合* @param size batch大小* List m: [a, b, c, d, e, f, g]* -> splitList(m, 3)* -> [[a, b, c], [d, e, f], [g]]* @return List<Batch>*/public static <T> List<List<T>> splitList(List<T> list, int size) {//集合為空時,返回nullif (list == null){return null;}//分割后的集合套集合List<List<T>> l1 = new ArrayList<>();//如果分割size>=集合size,那就沒必要分割if (list.size() <= size){l1.add(list);} else {//集合sizeint s = list.size();//x=新的集合套集合的sizeint x = s / size;//y=新的集合套集合最后一個子集合的sizeint y = s % size;if (y != 0){x = x + 1;}int index = 0;for (int i = 0; i < x; i++) {List<T> l2 = new ArrayList<>();for (int j = 0; j < list.size(); j++) {//按原集合給subList裝元素,l2.add(list.get(index));index++;//當達到sublist個數size裝滿時,跳出循環if (l2.size() == size) {l1.add(l2);break;//最后一個subList時} else if (x == l1.size() + 1 && y == l2.size()) {l1.add(l2);break;}}}}return l1;}
}
數據庫批處理操作
if (bigList.size() > 0) {for (List<CustomTableBean> subList : ListUtils.splitList(bigList, 100)) {int rows = customMapper.insertAll(subList);log.info("insert data size is " + rows);try {Thread.sleep(50);} catch (InterruptedException e) {log.error(e.printStackTrace(););}}
}