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. Word:转换PDF

    本文适用于Word 2007 + Windows 7,造冰箱的大熊猫@cnblogs 2018/8/3 一.Word文档转PDF文档 把Word文档转换为PDF,有两个免费解决方案 1.Microso ...

  2. Scratch的入门笔记

    最近发现人工智能和编程在小学开始普及,由于好奇,所以开始去了解儿童编程方面的知识,希望增加一些儿童编程教育相关的知识面,跟上发展潮流. Scratch是一款由麻省理工学院的“终身幼儿园团队”(Life ...

  3. 如何在matalb图像上添加公式符号

    方法: legend({'$\sigma(t)$'},'interpreter','latex') 效果如下:

  4. getFieldDecorator用法(一)——登录表单

    之前使用antd的ui表单,却没发现这么好用的用法,推荐给大家 import React from "react"; import { Card, Form, Input, But ...

  5. apply,call,bind函数作用与用法

    作用 可以把方法借给其它对象使用,并且改变this的指向 a.apply(b,[3,2]);//this指向由a变为b, a的方法借给b使用 实例: function add(a,b){       ...

  6. 【学习】mysql 时间戳与日期格式的相互转换

    1.UNIX时间戳转换为日期用函数: FROM_UNIXTIME() ); 输出:2006-08-22 12:11:10 2.日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() Sel ...

  7. jpa hibernate mybatis

    jpa: entityManagerFactory: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean hibern ...

  8. CentOS 7,使用yum安装Nginx

    https://www.centos.bz/2018/01/centos-7%EF%BC%8C%E4%BD%BF%E7%94%A8yum%E5%AE%89%E8%A3%85nginx/ 文章目录 [隐 ...

  9. DB2日常管理

    执行时间最长的10条SQL语句(按时间降序排列),可保存为脚本方便调用:db2 "SELECT rows_read / (num_executions + 1) as avg_rows_re ...

  10. 百度地图api根据地址获取经纬度

    package com.haiyisoft.cAssistant;import java.io.BufferedReader;import java.io.IOException; import ja ...