批量操作的核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中一个,其它的就可以举一反三,触类旁通。它之所以执行效率高,是因为合并后日志量(MySQL的binlog和InnoDB的事务日志)减少了,降低日志刷盘的数据量和频率,从而提高效率;同时也能减少SQL语句解析的次数,减少网络传输的IO。但是,以下几点需要注意:
  1.  SQL语句有长度限制,在进行数据合并在同一SQL中务必不能超过SQL长度限制,通过max_allowed_packet配置可以修改,默认是1M。
  2.  事务需要控制大小,事务太大可能会影响执行的效率。MySQL有innodb_log_buffer_size配置项,超过这个值会把InnoDB的数据刷到磁盘中,这时,效率会有所下降。所以比较好的做法是,在数据达到这个值前进行事务提交。
      在《Java 使用线程池分批插入或者更新数据》中介绍了如何在Java端批量切分数据,然后,使用线程池将被切分的数据传入相应DAO层的方法后,即可完整实现批量操作。在《Mybatis批量insert 返回主键值和foreach标签详解》中已经介绍了批量插入操作,而且,详细描述了foreach标签,这里简要概述批量删除、更新和查找。
     首先,mysql需要数据库连接配置&allowMultiQueries=true
jdbc:mysql://127.0.0.1:3306/mybank?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

批量删除

<delete id= "batchDeleteByIds" parameterType= "list">
       delete from instance where instance_id in
       <foreach collection="list" item= "item" index ="index"
            open= "(" close =")" separator=",">
            #{item}
       </foreach >
</delete >
批量更新
<update id= "updateUpdateTimeByIds" parameterType= "map">
    update instance
    set update_time  = #{ updateTime } where instance_id in
    <foreach collection="idlist" item= "uid" index ="index"
            open= "(" close =")" separator=",">
            #{ uid}
     </foreach >
</update >
      用法和之前的基本相同,但是这里传入的参数是map类型,idlist和updateTime是map的key。
批量查询
<select id="selectByIds" resultType="list" parameterType="map"> 
 SELECT infos, create_time, update_time FROM instance WHERE instance_id in
     <foreach collection="ids" item="id" index="index" open="(" close=")" separator=","> 
           #{id}
     </foreach>
</select>
    这里提供一下DAO层:
List<Instance> selectByIds (Map<String, Object> map);
void batchDeleteByIds (List<Long> list);
void updateUpdateTimeByIds(Map<String, Object> map);
      乍看上去这个foreach没有问题,但是经过项目实践发现,当表的列数较多(20+),以及一次性插入的行数较多(5000+)时,整个插入的耗时十分漫长,达到了14分钟,这是不能忍的。在资料中也提到了一句话:

 
   Of course don't combine ALL of them, if the amount is HUGE. Say you have 1000 rows you need to insert, then don't do it one at a time. You shouldn't equally try to have all 1000 rows in a single query. Instead break it into smaller sizes.
      它强调,当插入数量很多时,不能把所有的鸡蛋放在同一个篮子里,即一次性全放在一条语句里。可是为什么不能放在同一条语句里呢?这条语句为什么会耗时这么久呢?我查阅了资料发现: 
   Insert inside MyBatis foreach is not batch, this is a single (could become giant) SQL statement and that brings drawbacks:
  • some database such as Oracle here does not support.
  • in relevant cases: there will be a large number of records to insert and the database configured limit (by default around 2000 parameters per statement) will be hit, and eventually possibly DB stack error if the statement itself become too large.
   Iteration over the collection must not be done in the mybatis XML. Just execute a simple Insertstatement in a Java Foreach loop. The most important thing is the session Executor type.
SqlSession session = sessionFactory.openSession(ExecutorType.BATCH);
for (Model model : list) {
session.insert("insertStatement", model);
}
session.flushStatements();
   Unlike default ExecutorType.SIMPLE, the statement will be prepared once and executed for each record to insert.
       虽然MyBatis官网推荐使用ExecutorType.BATCH 的插入方式,因为,其性能更好;但是,其SQL写在了Java里,如果SQL比较复杂,则不易于维护。因此,本文只详细介绍了常见的使用foreach标签的方式。下面从MyBatis官网借用一个Batch Insert
示例。 
      A batch insert is a collection of statements that can be used to execute a JDBC batch. A batch is the preferred method of doing bulk inserts with JDBC. The basic idea is that you configure the connection for a batch insert, then execute the same statement multiple times, with different values for each inserted record. MyBatis has a nice abstraction of JDBC batches that works well with statements generated from this library. A batch insert looks like this:
...
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
SimpleTableMapper mapper = session.getMapper(SimpleTableMapper.class);
List<SimpleTableRecord> records = getRecordsToInsert(); // not shown BatchInsert<SimpleTableRecord> batchInsert = insert(records)
.into(simpleTable)
.map(id).toProperty("id")
.map(firstName).toProperty("firstName")
.map(lastName).toProperty("lastName")
.map(birthDate).toProperty("birthDate")
.map(employed).toProperty("employed")
.map(occupation).toProperty("occupation")
.build()
.render(RenderingStrategy.MYBATIS3); batchInsert.insertStatements().stream().forEach(mapper::insert); session.commit();
} finally {
session.close();
}
...
Reference 

Mybatis 批量操作-删除、修改和查询的更多相关文章

  1. 转载Entity Framework 5.0(EF first)中的添加,删除,修改,查询,状态跟踪操作

    转载原出处:http://www.cnblogs.com/kenshincui/p/3345586.html Entity Framework将概念模型中定义的实体和关系映射到数据源,利用实体框架可以 ...

  2. MyBatis中实现多表查询

    如果查询的数据量大,推荐使用N+1次查询.数据量少使用联合查询... 一. 1.Mybatis是实现多表查询方式 1.1  业务装配:对两个表编写单表查询语句,在业务(Service)把查询的两表结果 ...

  3. Nodejs之MEAN栈开发(九)---- 用户评论的增加/删除/修改

    由于工作中做实时通信的项目,需要用到Nodejs做通讯转接功能,刚开始接触,很多都不懂,于是我和同事就准备去学习nodejs,结合nodejs之MEAN栈实战书籍<Getting.MEAN.wi ...

  4. 9_13学习完整修改和查询&&实体类,数据访问类

    完整修改和查询:中间变量运用. 1.先查 2.执行操作 ---------------------------------------------------- namespace ADO.NET_小 ...

  5. ADO.NET(完整修改和查询、实体类,数据访问类)

    一.完整修改和查询 在编写c#语句时需考虑到用户体验,例如在编写修改语句时,需要考虑到输入的内容在数据库中是否能够找到. 中间变量运用. 1.先查 2.执行操作 完整修改语句: bool has = ...

  6. SQL语句添加删除修改字段及一些表与字段的基本操作

    用SQL语句添加删除修改字段 1.增加字段     alter table docdsp    add dspcode char(200)2.删除字段     ALTER TABLE table_NA ...

  7. Mybatis oracle多表联合查询分页数据重复的问题

    Mybatis oracle多表联合查询分页数据重复的问题 多表联合查询分页获取数据时出现一个诡异的现象:数据总条数正确,但有些记录多了,有些记录却又少了甚至没了.针对这个问题找了好久,最后发现是由于 ...

  8. 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...

  9. SQL语句添加删除修改字段[sql server 2000/2005]

    用SQL语句添加删除修改字段1.增加字段     alter table docdsp    add dspcodechar(200)2.删除字段     ALTER TABLE table_NAME ...

随机推荐

  1. (一)WebService基础

    一.SOA架构 先给出一个概念 SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构,        既然说是一种架构的话,所以一般认为 SOA 是包 ...

  2. 浅析web网站反向代理的配置

    一.背景 最近在部署项目到web服务器上时,该项目有一个打开视频监控的功能,视频的服务器是一台内网的服务器,不允许设置外网端口访问,网站服务器和视频服务器在同一个局域网内,可以相互联通.网络拓扑图如下 ...

  3. VBA消息框(MsgBox)(五)

    MsgBox函数显示一个消息框,并等待用户点击一个按钮,然后根据用户点击的按钮执行相关的操作. 语法 MsgBox(prompt[,buttons][,title][,helpfile,context ...

  4. MySQL数字类型int与tinyint、float与decimal如何选择

    最近在准备给开发做一个mysql数据库开发规范方面培训,一步一步来,结合在生产环境发现的数据库方面的问题,从几个常用的数据类型说起. int.tinyint与bigint 它们都是(精确)整型数据类型 ...

  5. Vue下的index.html

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. tensorflow实战笔记(19)----使用freeze_graph.py将ckpt转为pb文件

    一.作用: https://blog.csdn.net/yjl9122/article/details/78341689 这节是关于tensorflow的Freezing,字面意思是冷冻,可理解为整合 ...

  7. HDU 6735 结论网络流 机器人不重叠路径

    我们可以得到一个结论:没有两个机器人走过的路会重叠 所以题目就转变为了能不能让机器人的路径不重叠且每个机器人能到达终点 直接一个点朝他四连通方向的四个点连容量为1的边即可 #include<bi ...

  8. Web应用中的缓存一致性问题

    上篇总结了缓存中出现频率比较高的一些问题,今天详细说说web应用中的缓存一致性问题. 主要说以下三个方面 数据库与缓存中数据不一致出现的情形 发生不一致时的优化思路 如何保证数据库与缓存的一致性 先来 ...

  9. Disconnected from the target VM, address: '127.0.0.1:56577', transport: 'socket'

    Disconnected from the target VM, address: '127.0.0.1:56577', transport: 'socket' Disconnected from t ...

  10. re模块中的非贪婪匹配

    python的re模块中有贪婪匹配和非贪婪匹配之分,当使用*时会匹配零个或多个,使用+时会匹配一个或多个.当使用?在前边特殊符号前时会进行非贪婪匹配,匹配零个或者一个,今天主要讨论非贪婪匹配中存在的坑 ...