mybatis的执行器有三种类型:

  • ExecutorType.SIMPLE

这个类型不做特殊的事情,它只为每个语句创建一个PreparedStatement。

  • ExecutorType.REUSE

这种类型将重复使用PreparedStatements。

  • ExecutorType.BATCH

这个类型批量更新,且必要地区别开其中的select 语句,确保动作易于理解。

可以在配置sqlSession时指定相应的类型:

  1. <bean id="fsasSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  2. <constructor-arg index="0" ref="fsasSqlSessionFactory" />
  3. <constructor-arg index="1" value="SIMPLE" />
  4. </bean>

也可以在通过SqlSessionFactory创建一个SqlSession时指定:

  1. sqlSessionFactory.openSession(ExecutorType.BATCH);

openSession有很多方式:

  1. SqlSession openSession()
  2. SqlSession openSession(boolean autoCommit)
  3. SqlSession openSession(Connection connection)
  4. SqlSession openSession(TransactionIsolationLevel level)
  5. SqlSession openSession(ExecutorType execType,TransactionIsolationLevel
  6. level)
  7. SqlSession openSession(ExecutorType execType)
  8. SqlSession openSession(ExecutorType execType, boolean autoCommit)
  9. SqlSession openSession(ExecutorTyp

默认执行器是SIMPLE。

三种类型执行器除了上面的特点外,在使用过程中还发现:

  • ExecutorType.SIMPLE:可以返回自增键,只需要在mapper文件中,增加属性: useGeneratedKeys="true" keyProperty="productId"

    1. <!-- 插入一个user -->
    2. <insert id="insertUser" parameterType="User"
    3. statementType="PREPARED" useGeneratedKeys="true" keyProperty="userId">
    4. INSERT
    5. INTO user (
    6. <include refid="userColumns" />
    7. , create_time,
    8. update_time)
    9. VALUES
    10. (#{email}, #{pwd},#{nickname},
    11. #{phone}, #{sign}, #{age},
    12. #{birthday},
    13. #{createTime},
    14. now())
    15. </insert>

    那么自增键会在事务提交后,自动设置到传入的user对象中

  • ExecutorType.BATCH:当前最新版本的mybatis(mybatis-3.2.0)无法再返回自增键值,只返回最后一个更新记录的自增键值(基本上没上意义)。并且无法返回更新数据的记录数
  • 要实现批量插入数据有两种方式:
  1. 使用SIMPLE执行器,借助foreach动态sql语句,使用Insert values(...),(...),(...) 的方式,这种方式无法取到自增键

    比如

    1. <!-- 批量插入user -->
    2. <insert id="insertUsers" parameterType="map" useGeneratedKeys="true"
    3. keyProperty="userId">
    4. INSERT
    5. INTO user (
    6. <include refid="userColumns" />
    7. , create_time,
    8. update_time)
    9. VALUES
    10. <foreach collection="users" item="userCommand" index="index"
    11. separator=",">
    12. (#{userCommand.email},
    13. #{userCommand.pwd},#{userCommand.nickname},
    14. #{userCommand.phone},
    15. #{userCommand.sign}, #{userCommand.age},
    16. #{userCommand.birthday},
    17. #{userCommand.sex},
    18. #{userCommand.createTime},
    19. now())
    20. </foreach>
    21. </insert>
  2. 使用BATCH执行器,但是SqlSession的执行器类型一旦设置就无法动态修改,所以如果在配置文件中设置了执行器为SIMPLE,当要使用BATCH执行器时,需要临时获取:
    1. SqlSession session = sqlSessionTemplate.getSqlSessionFactory()
    2. .openSession(ExecutorType.BATCH, false);
    3. try {
    4. UserDao batchUserDao = session.getMapper(UserDao.class);
    5. for (UserCommand user : users) {
    6. batchUserDao.insertUser(user);
    7. }
    8. session.commit();
    9. // 清理缓存,防止溢出
    10. session.clearCache();
    11. // 添加位置信息
    12. userLbsDao.insertUserLbses(users);
    13. } finally {
    14. session.close();
    15. }

    这个方法仍然需要包在事务中

 
 转自:http://blog.csdn.net/meiwen1111/article/details/8260387

mybatis使用的一点小结:session运行模式及批量提交(转)的更多相关文章

  1. Spark学习之路(五)—— Spark运行模式与作业提交

    一.作业提交 1.1 spark-submit Spark所有模式均使用spark-submit命令提交作业,其格式如下: ./bin/spark-submit \ --class <main- ...

  2. Spark 系列(五)—— Spark 运行模式与作业提交

    一.作业提交 1.1 spark-submit Spark 所有模式均使用 spark-submit 命令提交作业,其格式如下: ./bin/spark-submit \ --class <ma ...

  3. Mybatis之基础应用小结以及IntelliJ IDEA目录结构的一些小问题

    IntelliJ IDEA 目录结构的一些小问题 [Mybatis 之基础应用小结] 1.不管怎么样,先建立一个简单的MySQL数据表,如下所示 2.接下来要做的事情就是通过Mybatis对数据表进行 ...

  4. Apache Flink on K8s:四种运行模式,我该选择哪种?

    1. 前言 Apache Flink 是一个分布式流处理引擎,它提供了丰富且易用的API来处理有状态的流处理应用,并且在支持容错的前提下,高效.大规模的运行此类应用.通过支持事件时间(event-ti ...

  5. PHP运行模式

    1.运行模式 关于PHP目前比较常见的五大运行模式: 1)CGI(通用网关接口 / Common Gateway Interface) 2)FastCGI(常驻型CGI / Long-Live CGI ...

  6. php运行模式的比较(转)

    PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli  命令行运行   ( ...

  7. 【转载】PHP运行模式的深入理解

    PHP运行模式的深入理解 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-03我要评论 本篇文章是对PHP运行模式进行了详细的分析介绍,需要的朋友参考下   PHP运行模式有4钟:1) ...

  8. ARM处理器的寄存器,ARM与Thumb状态,7中运行模式 【转】

    转自:http://blog.chinaunix.net/uid-28458801-id-3494646.html ARM处理器工作模式一共有 7 种 : USR  模式    正常用户模式,程序正常 ...

  9. PHP运行模式的深入理解

    PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli  命令行运行   ( ...

随机推荐

  1. 盒子模型的overflow属性,border属性,padding与margin属性

    今天要写的是CSS布局—盒子模型 首先说一下CSS的整体布局: 它包括容器(container),页眉(header),导航条(navbar),页面主要内容(main),菜单(menu),主要内容(c ...

  2. 1、Qt应用程序

    新建Qt Widgets Application,基类选择QWidget Qt项目特点(参考上图):头文件名与类名一样,成对出现 main.cpp代码解释如下 #include "mywid ...

  3. 大视频上传T级别解决方案

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  4. C/C++预处理指令#define,条件编译#ifdefine

    本文主要记录了C/C++预处理指令,常见的预处理指令如下: #空指令,无任何效果 #include包含一个源代码文件 #define定义宏 #undef取消已定义的宏 #if如果给定条件为真,则编译下 ...

  5. [CF1223G/1240E]Wooden Raft 题解

    前言 上午一场模拟赛(发布前很久,因为这题题解太长了),发现T3特别珂怕,打开题解,发现一行字: 不要再问了,再问就是CF 1240E 当场去世.jpg. 在下文中,我们记 \(A\) 为 \(a\) ...

  6. Libraries&Workflow for a modern geospatial processing(现代地理空间处理的库与工作流)

    Libraries for a modern geospatial workflow现代地理空间工作的类库 Distribution Writing, Running, and Distributin ...

  7. (Java多线程系列六)join()的用法和线程的优先级

    join()的用法和线程的优先级 1.join()的用法 join()作用就是让其他线程处于等待状态 先看一个需求:创建一个线程,子线程执行完毕后,主线程才能执行 public class JoinT ...

  8. python学习之路(4)

    使用list和tuple Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >& ...

  9. .item布局设置分割线

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  10. EBS 显示主页面的工作列表和主菜单

    EBS环境: R12.1.3 问题描述:如果系统的“个性化页”做了设置,可能出现登录系统后,如果下图红框中的 主菜单和工作列表没有显示的情况,如果需要重新显示“主菜单”和“工作列表”,可参考以下操作 ...