zoukankan      html  css  js  c++  java
  • 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){
                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 ;
        }
  • 相关阅读:
    C#开发Unity游戏教程之游戏对象的属性变量
    C#开发Unity游戏教程之Scene视图与脚本的使用
    BeagleBone Black教程之BeagleBone Black使用到的Linux基础
    BeagleBone Black教程之BeagleBone Black设备的连接
    ARP协议详解之Gratuitous ARP(免费ARP)
    ARP协议详解之ARP动态与静态条目的生命周期
    ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge
    ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun
    ArduinoYun教程之配置Arduino Yun环境
    IIR数字滤波器
  • 原文地址:https://www.cnblogs.com/wudi521/p/9830066.html
Copyright © 2011-2022 走看看