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 ;
    }

猜你喜欢

转载自www.cnblogs.com/wudi521/p/9830066.html
今日推荐