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. mysql报错:Cause: java.sql.SQLException: sql injection violation, syntax error: ERROR. pos 39, line 2, column 24, token CLOSE

    因为close是mysql关键字 -- ::, DEBUG (BaseJdbcLogger.java:)- ==> Preparing: , -- ::, INFO (XmlBeanDefini ...

  2. jquery attribute$=value选择器 语法

    jquery attribute$=value选择器 语法 作用:[attribute$=value] 选择器选取每个带有指定属性且以指定字符串结尾的元素. 语法:$("[attribute ...

  3. TTTTTTTTTTTTTTT poj 2932 Coneology 平面扫描+STL

    题目链接 题意:有n个圆,圆之间不存在相交关系,求有几个不被其他任何圆包含的圆,并输出圆的编号: #include <iostream> #include <cstdio> # ...

  4. jQuery_CSS类操作

    下面讲述jQuery操作css类,进行类的添加,移除,以及前两项功能的结合操作. <!DOCTYPE html> <html> <head> <meta ch ...

  5. (转载)rabbitmq与springboot的安装与集成

    原文地址:https://segmentfault.com/a/1190000016991529 一.前言 RabbitMQ是一个开源的消息代理软件(面向消息的中间件),它的核心作用就是创建消息队列, ...

  6. [NLP] The Annotated Transformer 代码修正

    1. RuntimeError: "exp" not implemented for 'torch.LongTensor' class PositionalEncoding(nn. ...

  7. HDU 5793 A Boring Question (找规律 : 快速幂+逆元)

    A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...

  8. [CSP-S模拟测试]:多维网格(组合数学+容斥)

    题目传送门(内部题138) 输入格式 输入数据第一行为两个整数$d,n$. 第二行$d$个非负整数$a_1,a_2,...,a_d$.     接下来$n$行,每行$d$个整数,表示一个坏点的坐标.数 ...

  9. Promise 的使用

    Promise 的使用,用于异步处理 ,以及解决地狱回调的: 1.  Promise 是一个构造函数,既然是构造函数,我们就可以 new Promise() 就可以得到一个 Promise 的实例 2 ...

  10. express 模板 及 文件上传

    express 的三大功能: 1. 提供了静态服务(所谓的根目录) let express = require("express"); let app = express(); a ...