替代saveBatch

  public Boolean extendDetailUser(SalaryAccountingItemParam param){
//获取上期已选人员关联的项目
List<SalaryAccountingItemDetailUser> extendDetailUserList = this.list(Wrappers.<SalaryAccountingItemDetailUser>lambdaQuery()
.eq(SalaryAccountingItemDetailUser::getSalaryAccountingId, param.getLastSalaryAccountingId())
.eq(SalaryAccountingItemDetailUser::getYearMonth, param.getLastYearMonth()));
if (extendDetailUserList.isEmpty()) {
return true; // 如果没有数据,则直接返回 true
}
//大于1000条分批保存
int batchStart = 0;
int batchEnd;
while ((batchEnd = Math.min(batchStart + 1000, extendDetailUserList.size())) > batchStart) {
List<SalaryAccountingItemDetailUser> batch = extendDetailUserList.subList(batchStart, batchEnd);
StringBuilder sqlBuilder = new StringBuilder("INSERT INTO salary_accounting_item_detail_user ( idCard, itemDetailId, yearMonth, salaryAccountingId, userName ) VALUES ");
for (SalaryAccountingItemDetailUser salaryAccountingItemDetailUser : batch){
sqlBuilder.append("('");
sqlBuilder.append(salaryAccountingItemDetailUser.getIdCard());
sqlBuilder.append("',");
sqlBuilder.append(salaryAccountingItemDetailUser.getItemDetailId());
sqlBuilder.append(",'");
sqlBuilder.append(param.getYearMonth());
sqlBuilder.append("',");
sqlBuilder.append(param.getId());
sqlBuilder.append(",'");
sqlBuilder.append(salaryAccountingItemDetailUser.getUserName());
sqlBuilder.append("'),");
}
// 移除最后一个逗号
sqlBuilder.setLength(sqlBuilder.length() - 1);
jdbcTemplate.batchUpdate(sqlBuilder.toString()); // 执行批量操作
batchStart = batchEnd;
}
return true;
}
posted @   八英里  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示