分批插入简洁代码
public int saveLatestKeyword(List<BaiduCpcKeywords> keywordInfoList, Long userId) {
int count = 0;
int size = keywordInfoList.size();
if (size > 0) {
// 分批插入
int maxNum = 1000;
List<BaiduCpcKeywords> tempList = null;
int i = 0;
do {
int toIndex = i + maxNum < size ? i + maxNum : size;
if (i >= toIndex) break;
tempList = keywordInfoList.subList(i, toIndex);
count += baiduCpcKeywordsDAO.saveBaiduCpcKeywordsBatch(tempList);
i += maxNum;
} while (tempList.size() == maxNum);
if (count != keywordInfoList.size()) {
logger.warn("【SEM-批量插入百度推广数据】批量插入推广关键词数据出错");
}
}
return count;
}
拆分集合工具
guava工具类
List<User> users = userService.findAll();
//按每50个一组分割
List<List<User>> parts = Lists.partition(users, 50);
parts.stream().forEach(list -> {
process(list);
});