当list太大的时候,无法一次性存储到数据库中,可分割为多个list,分批次插入数据库
List<DqEmpiricalDocumentReportLog> logParamList = new ArrayList<DqEmpiricalDocumentReportLog>();
//按500进行分割入库
List<List<DqEmpiricalDocumentReportLog>> partition = Lists.partition(logParamList, CommonConstant.OPERATE_LOG_INSERT_SIZE);
for (List<DqEmpiricalDocumentReportLog> one : partition){
int i = dqEmpiricalDocumentReportLogMapper.inserEmpDocReportLog(one);
if (i != one.size()) throw new Exception("数据质量实证材料报告数据操作日志保存失败!");
};
将list集合按指定长度进行切分,返回新的List<List<??>>集合,如下的:
List<List<Integer>> lists=Lists.partition(numList,3);
package test;
import com.google.common.collect.Lists;
import org.junit.Test;
import java.util.List;
/**
* Created by Administrator on 2018/10/28/028.
*/
public class testList {
@Test
public void test(){
List<Integer> numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> lists=Lists.partition(numList,3);
System.out.println(lists);//[[1, 2, 3], [4, 5, 6], [7, 8]]
}
}