最近有个需求,就是批量处理数据,但是并发量应该很大,当时第一时间想到得是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){
object = new Object[]{
flowCarReportDayBo.getId(),
flowCarReportDayBo.getPark_number(),
flowCarReportDayBo.getPark_name(),
flowCarReportDayBo.getStart_time(),
flowCarReportDayBo.getNature_sum_count(),
flowCarReportDayBo.getTemp_car_count(),
flowCarReportDayBo.getVip_car_count(),
flowCarReportDayBo.getIn_car_count(),
flowCarReportDayBo.getOut_car_count(),
flowCarReportDayBo.getCharge_sum_count(),
flowCarReportDayBo.getCharge_car_count(),
flowCarReportDayBo.getFree_car_count(),
flowCarReportDayBo.getDiscount_sum_count(),
flowCarReportDayBo.getDiscount_local_car_count(),
flowCarReportDayBo.getDiscount_bussiness_car_count(),
flowCarReportDayBo.getVisit_in_car_count(),
flowCarReportDayBo.getVisit_out_car_count(),
flowCarReportDayBo.getBlack_in_car_count(),
flowCarReportDayBo.getBlack_out_car_count(),
};
list.add(object);
} return list ;
}

最新文章

  1. setTimeout,setInterval原理
  2. 使用eclipse开发Java web应用
  3. server-pc---------------&gt;lspci,lsusb,meminfo等配置信息
  4. 【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。
  5. NGUI 3.5教程(六)Font字体
  6. [AHOI2015 Junior] [Vijos P1943] 上学路上 【容斥+组合数】
  7. 跑马灯js
  8. 四种方法解析JSON数据
  9. Java解析和生成XML
  10. css属性之box-shadow
  11. w530 在ubuntu 12.04 _x64 背光调节方法
  12. 【MongoDb基础】插入数据
  13. Java并发编程实践读书笔记(1)线程安全性和对象的共享
  14. [国嵌攻略][119][Linux中断处理程序设计]
  15. cisco模拟器GNS3和虚拟机VMware的整合
  16. Audio播放
  17. OK Titlefasdf asd
  18. Metasploit Framework(1)基本命令、简单使用
  19. 微信小程序Promise对象
  20. LED灯珠散热的计算方法

热门文章

  1. Java基础之入门
  2. Shell中数组的使用
  3. ASP.NET Core 2.1 : 十四.静态文件与访问授权、防盗链
  4. VMware 安装 centos6.8
  5. 微信中如何做到访问app的下载链接时直接跳到默认浏览器去执行下载
  6. PY3_线程红绿灯
  7. Linux常用命令之Tmux
  8. PHP变量传值赋值和引用赋值,变量销毁
  9. laravel 多条件查询
  10. 网络流学习(转载自ssw 的博客)