原 java stream对大的list进行切分,分批新增到数据库
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1419
int BATCH_COUNT=1000;
List<CustomerFlow> list=customerFlowMap.selectAllCustomerFlow();
int limit = (list.size() + BATCH_COUNT - 1) / BATCH_COUNT;
// 分成limit次发请求到数据库, in()操作时, 可以把多条数据分割成多组请求
Stream.iterate(0, n -> n + 1).limit(limit).forEach(h -> {
List<CustomerFlow> batchList=list.stream().skip(h * BATCH_COUNT).limit(BATCH_COUNT).collect(Collectors.toList());
int add=customerFlowMapper.batchInsert(batchList);
log.info("新增用户信息: {}", add);
if(add==0){
throw new BusinessException(BusinessUtils.CODE_201, BusinessUtils.API_MSG_1209);
}
batchList.clear(); // 清空
});
参考资料:
1)https://blog.csdn.net/fzy629442466/article/details/84765070
2)https://blog.csdn.net/CJYJP/article/details/105966647
2023-02-17 17:48:24 阅读(507)
名师出品,必属精品 https://www.91mszl.com
博主信息