直接貼代碼,主要運用 spa
List<E> subList(int fromIndex, int toIndex); 把 List 分割
/** * 保存批價結果 * * @param priceResult 批價結果 * @throws Exception 異常 */ private void savePriceResult(List<AmountPo> priceResult) throws Exception { if (CheckUtils.isNotEmpty(priceResult)) {
//很大的一個list 好比:20W條數據 int size = priceResult.size(); int startIndex = 0;
//每批入庫list大小 好比:5000條一批 int endIndex = (int) MAX_SIZE;
//定義臨時list List<AmountPo> amountPos; //分批入庫 while (true) { endIndex = endIndex > size ? size : endIndex;
//list.subList 分割成小的list amountPos = priceResult.subList(startIndex, endIndex); if (amountPos.size() == MAX_SIZE) {
//執行入庫操做 amountDao.batchAdd(amountPos); } else { //最後一批入庫數據 if (CheckUtils.isNotEmpty(amountPos)) { amountDao.batchAdd(amountPos); } break; } startIndex = endIndex; endIndex = (int) (startIndex + MAX_SIZE); } } }