java数据分批插入处理
作者:互联网
List<ComponentTo> componentList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(componentList)) {
// 数据总数
int totalCount = componentList.size();
// 页大小
int pageSize = 1000;
// 分批上传数据
int lastPageSize = totalCount % pageSize;
// 总页数
int totalPage = lastPageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
for (int p = 1; p <= totalPage; p++) {
if (lastPageSize == 0) {
componentDao.addAutoList(componentList.subList((p - 1) * pageSize, pageSize * p));
} else {
if (p == totalPage) {
componentDao.addAutoList(componentList.subList((p - 1) * pageSize, totalCount));
} else {
componentDao.addAutoList(componentList.subList((p - 1) * pageSize, pageSize * p));
}
}
TimeUnit.MILLISECONDS.sleep(50);
}
}
标签:componentList,lastPageSize,componentDao,java,pageSize,totalCount,int,分批,插入 来源: https://blog.csdn.net/qq_25423997/article/details/114112890