jdbcTemplate批量插⼊处理数据
最近有个需求,就是批量处理数据,但是并发量应该很⼤,当时第⼀时间想到得是mybatis的foreach去处理,但是后来通过查资料发现,相对有spring 的jdbcTemplate处理速度,mybatis还是有些慢,后来就⾃⼰重写了⼀下jdbcTemplate的批量处理代码:
public void batchCarFlowInsert(List<FlowCarReportDayBo> list) {
String sql =" INSERT INTO flow_report_day (id, park_number, park_name, start_time, nature_sum_count, " +
" temp_car_count, vip_car_count, in_car_count, out_car_count, charge_sum_count, charge_car_count, " +
" free_car_count, discount_sum_count, discount_local_car_count, discount_bussiness_car_count, " +
" visit_in_car_count, visit_out_car_count, black_in_car_count, black_out_car_count) " +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
List<Object[]> args = transformFlowCarReportDayBoToObjects(list);
int fromIndex = 0; int toIndex = BATCH_SIZE;
while (fromIndex != args.size()) {
if (toIndex > args.size()) {
toIndex = args.size();
}
this.jdbcTemplate.batchUpdate(sql,args.subList(fromIndex, toIndex));
fromIndex = toIndex;
toIndex += BATCH_SIZE;
if (toIndex > args.size())
toIndex = args.size();
}
}
最主要是的是将List<bean>转换为List<Object[]> :
private List<Object[]> transformFlowCarReportDayBoToObjects(List<FlowCarReportDayBo> flowCarReportDayBoList) {
List<Object[]> list = new ArrayList<>();
Object[] object = null;
for(FlowCarReportDayBo flowCarReportDayBo :flowCarReportDayBoList){
jdbctemplate查询一条数据object = new Object[]{
};
list.add(object);
}
return list ;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论