针对BatchPreparedStatementSetter的代码

本文介绍了一种使用JdbcTemplate进行批量数据插入的方法,通过提供具体的Java代码示例来展示如何高效地批量处理数据库操作。

批量处理用的很少,做一个记录




/**
* 批量更新数据

* @param forums
*/
public void addForums(final List<Forum> forums) {
final String sql = "INSERT INTO t_forum(forum_name,forum_desc) VALUES(?,?)";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
public int getBatchSize() {
return forums.size();
}


public void setValues(PreparedStatement ps, int index)
throws SQLException {
Forum forum = forums.get(index);
ps.setString(1, forum.getForumName());
ps.setString(2, forum.getForumDesc());
}
});
}


/** * @author lyp 2025/9/17 16:58 * 批量保存通知历史 */ private void saveBatchNotificationHistory(List<QHandleInfoDto> qHandleInfoDtos, Integer isSuccess, QMsgNotificationMethodEnum notificationMethodEnum,String content,UserDto userDto) { if(notificationMethodEnum==null){ return; } switch (notificationMethodEnum) { case SMS: // SMS处理逻辑 break; case EMAIL: // EMAIL处理逻辑 break; case API: // API处理逻辑 break; case WEBHOOK: // WEBHOOK处理逻辑 this.saveWebhookNotificationHistory(qHandleInfoDtos, isSuccess,notificationMethodEnum,content,userDto); break; case WECOM: // 企微个人 this.saveWecomNotificationHistory(qHandleInfoDtos, isSuccess,notificationMethodEnum,userDto); break; default: // 默认处理逻辑 break; } } private void saveWebhookNotificationHistory(List<QHandleInfoDto> qHandleInfoDtos, Integer isSuccess, QMsgNotificationMethodEnum notificationMethodEnum, String content, UserDto userDto) { for (QHandleInfoDto qHandleInfoDto : qHandleInfoDtos) { QHandleNotificationHistory history = new QHandleNotificationHistory(); history.setqId(qHandleInfoDto.getId()); history.setPnr(qHandleInfoDto.getPnr()); history.setOfficeNo(qHandleInfoDto.getOfficeNo()); history.setCustomerId(qHandleInfoDto.getCustomerId()); history.setCustomerName(qHandleInfoDto.getCustomerName()); history.setNotificationMethod(notificationMethodEnum.getValue()); history.setContent(content); history.setCreatedBy(userDto.getUserId()); history.setCreatedByName(userDto.getUserName()); qHandleNotificationHistoryMapper.insertSelective(history); } } private void saveWecomNotificationHistory(List<QHandleInfoDto> qHandleInfoDtos, Integer isSuccess, QMsgNotificationMethodEnum notificationMethodEnum,UserDto userDto) { for (QHandleInfoDto qHandleInfoDto : qHandleInfoDtos) { QHandleNotificationHistory history = new QHandleNotificationHistory(); history.setqId(qHandleInfoDto.getId()); history.setPnr(qHandleInfoDto.getPnr()); history.setOfficeNo(qHandleInfoDto.getOfficeNo()); history.setCustomerId(qHandleInfoDto.getCustomerId()); history.setCustomerName(qHandleInfoDto.getCustomerName()); history.setNotificationMethod(notificationMethodEnum.getValue()); history.setNotificationMethodValue(qHandleInfoDto.getCreateUserId()); history.setContent(this.generateWechatOperateInfo(qHandleInfoDto,isSuccess)); history.setCreatedBy(userDto.getUserId()); history.setCreatedByName(userDto.getUserName()); qHandleNotificationHistoryMapper.insertSelective(history); } } 代码优化
09-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值