最近有个需求,就是批量处理数据,但是并发量应该很大,当时第一时间想到得是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 ;
}

jdbcTemplate批量插入处理数据的更多相关文章

  1. SQL-35 对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作

    题目描述 对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作CREATE TABLE IF NOT EXISTS actor (actor_id smallint(5 ...

  2. SQL-34 对于表actor批量插入如下数据

    题目描述 对于表actor批量插入如下数据CREATE TABLE IF NOT EXISTS actor (actor_id smallint(5) NOT NULL PRIMARY KEY,fir ...

  3. JdbcTemplate批量插入数据

    运行环境:SpringBoot,注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; 1.单表批量插入数据 @Test public ...

  4. .Net批量插入数据到SQLServer数据库,System.Data.SqlClient.SqlBulkCopy类批量插入大数据到数据库

    批量的的数据导入数据库中,尽量少的访问数据库,高性能的对数据库进行存储. 采用SqlBulkCopy来处理存储数据.SqlBulkCopy存储大批量的数据非常的高效,将内存中的数据表直接的一次性的存储 ...

  5. ADO.NET 新特性之SqlBulkCopy(批量插入大量数据)

    转自:http://blog.csdn.net/huaer1011/article/details/2312361 在.Net1.1中无论是对于批量插入整个DataTable中的所有数据到数据库中,还 ...

  6. .net使用SqlBulkCopy类操作DataTable批量插入数据库数据,然后分页查询坑

    在使用SqlBulkCopy类操作DataTable批量插入数据,这种操作插入数据的效率很高,就会导致每一条数据在保存的时间基本一样,在我们分页查询添加的数据是,使用数据的添加时间来排序就会出现每页的 ...

  7. spring JdbcTemplate批量插入以及单个插入时获取id

    1. 批量更新插入 jdbcTemplate.batchUpdate(String sql, List<Object[]> batchArgs) Object[]数组的长度为每条记录的参数 ...

  8. oracle 快速批量插入复杂数据的内容

    最近迷上一种批量插入的方法,一句sql解决,将需要插入的数据用with as 的方式查出来,不管多么复杂的sql,都可以用临时表的方式查出来,然后直接插入,这样代码更加清晰 流程也简单 insert ...

  9. JDBC批量插入blob数据

    图片从接口读取后是base64的字符串,所以转成byte数组进行保存. 我们一般保存数据的话,都是基本数据,对于这些图片数据大部分会将图片保存成Blob,Clob等. Blob存储的是二进制对象数据( ...

随机推荐

  1. C语言面试基础知识整理

    一.预处理 1.什么是预编译?何时需要预编译? (1)预编译又称预处理,是做些代码文本的替换工作,即程序执行前的一些预处理工作.主要处理#开头的指令,如拷贝#include包含的文件代码.替换#def ...

  2. 【原】无脑操作:IDEA热部署设置

    热部署的概念:在应用正在运行的时候升级软件,却不需要重新启动应用.对于Java应用程序来说,热部署就是在运行时更新Java类文件. 注意:经过试验,IDEA 2017可以使用热部署,IDEA 14不行 ...

  3. Zabbix3.4-RHEL 7.4 X64 YUM联网安装

    OS准备 关闭selinux vi /etc/selinux/config setenforce 0 开启防火墙80端口访问 firewall-cmd --permanent --add-rich-r ...

  4. 我的第一个python web开发框架(37)——职位管理功能

    对于职位管理,我们可以理解它为角色权限的管理,就像前面所说的一样,有了职位管理,后台管理系统绑定好对应的权限以后,新进员工.离职或岗位调整,管理员操作起来就非常的便捷了,只需要重新绑定对应职位就可以做 ...

  5. [已解决]报错:Required request body is missing

    问题代码: res = requests.post(getXxxxList_url, headers=headers, data={}) 对象网站: angular4 apache 通过验证 (coo ...

  6. Jquery mobile中用Jquery的append()追加的内容没有Jquery mobile的样式

    Jquery Mobile 动态添加块之后, 样式不是JM内定的样式,解决方案如下: $('#content').append(html).enhanceWithin();//Jquery Mobil ...

  7. js 对象 类型转换

    对象不相等 var o = {x: 1}, p = {x: 1}; console.log(o == p); console.log(o === p); var arr1 = [], arr2 = [ ...

  8. 简单解析nestJS目录

    使用Nest CLI设置新项目非常简单 .只需确保 安装了npm,然后在OS终端中使用以下命令: $ npm i -g @nestjs/cli $ nest new project-name $ cd ...

  9. vue 倒计时组件

    <template> <span> <i v-text="msg"></i> </span></template& ...

  10. VisualStudio神级插件Resharper技巧基础入门到骨灰玩家使用全教程+Resharper性能优化

    原文地址:https://www.masuit.com/21/resharper 破解地址:https://www.masuit.com/20/resharper 官方文档:https://www.j ...