mybatis使用的一点小结:session运行模式及批量提交(转)
mybatis的执行器有三种类型:
- ExecutorType.SIMPLE
这个类型不做特殊的事情,它只为每个语句创建一个PreparedStatement。
- ExecutorType.REUSE
这种类型将重复使用PreparedStatements。
- ExecutorType.BATCH
这个类型批量更新,且必要地区别开其中的select 语句,确保动作易于理解。
可以在配置sqlSession时指定相应的类型:
- <bean id="fsasSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="fsasSqlSessionFactory" />
- <constructor-arg index="1" value="SIMPLE" />
- </bean>
也可以在通过SqlSessionFactory创建一个SqlSession时指定:
- sqlSessionFactory.openSession(ExecutorType.BATCH);
openSession有很多方式:
- SqlSession openSession()
- SqlSession openSession(boolean autoCommit)
- SqlSession openSession(Connection connection)
- SqlSession openSession(TransactionIsolationLevel level)
- SqlSession openSession(ExecutorType execType,TransactionIsolationLevel
- level)
- SqlSession openSession(ExecutorType execType)
- SqlSession openSession(ExecutorType execType, boolean autoCommit)
- SqlSession openSession(ExecutorTyp
默认执行器是SIMPLE。
三种类型执行器除了上面的特点外,在使用过程中还发现:
- ExecutorType.SIMPLE:可以返回自增键,只需要在mapper文件中,增加属性: useGeneratedKeys="true" keyProperty="productId"
- <!-- 插入一个user -->
- <insert id="insertUser" parameterType="User"
- statementType="PREPARED" useGeneratedKeys="true" keyProperty="userId">
- INSERT
- INTO user (
- <include refid="userColumns" />
- , create_time,
- update_time)
- VALUES
- (#{email}, #{pwd},#{nickname},
- #{phone}, #{sign}, #{age},
- #{birthday},
- #{createTime},
- now())
- </insert>
那么自增键会在事务提交后,自动设置到传入的user对象中
- ExecutorType.BATCH:当前最新版本的mybatis(mybatis-3.2.0)无法再返回自增键值,只返回最后一个更新记录的自增键值(基本上没上意义)。并且无法返回更新数据的记录数
- 要实现批量插入数据有两种方式:
- 使用SIMPLE执行器,借助foreach动态sql语句,使用Insert values(...),(...),(...) 的方式,这种方式无法取到自增键
比如
- <!-- 批量插入user -->
- <insert id="insertUsers" parameterType="map" useGeneratedKeys="true"
- keyProperty="userId">
- INSERT
- INTO user (
- <include refid="userColumns" />
- , create_time,
- update_time)
- VALUES
- <foreach collection="users" item="userCommand" index="index"
- separator=",">
- (#{userCommand.email},
- #{userCommand.pwd},#{userCommand.nickname},
- #{userCommand.phone},
- #{userCommand.sign}, #{userCommand.age},
- #{userCommand.birthday},
- #{userCommand.sex},
- #{userCommand.createTime},
- now())
- </foreach>
- </insert>
- 使用BATCH执行器,但是SqlSession的执行器类型一旦设置就无法动态修改,所以如果在配置文件中设置了执行器为SIMPLE,当要使用BATCH执行器时,需要临时获取:
- SqlSession session = sqlSessionTemplate.getSqlSessionFactory()
- .openSession(ExecutorType.BATCH, false);
- try {
- UserDao batchUserDao = session.getMapper(UserDao.class);
- for (UserCommand user : users) {
- batchUserDao.insertUser(user);
- }
- session.commit();
- // 清理缓存,防止溢出
- session.clearCache();
- // 添加位置信息
- userLbsDao.insertUserLbses(users);
- } finally {
- session.close();
- }
这个方法仍然需要包在事务中
mybatis使用的一点小结:session运行模式及批量提交(转)的更多相关文章
- Spark学习之路(五)—— Spark运行模式与作业提交
一.作业提交 1.1 spark-submit Spark所有模式均使用spark-submit命令提交作业,其格式如下: ./bin/spark-submit \ --class <main- ...
- Spark 系列(五)—— Spark 运行模式与作业提交
一.作业提交 1.1 spark-submit Spark 所有模式均使用 spark-submit 命令提交作业,其格式如下: ./bin/spark-submit \ --class <ma ...
- Mybatis之基础应用小结以及IntelliJ IDEA目录结构的一些小问题
IntelliJ IDEA 目录结构的一些小问题 [Mybatis 之基础应用小结] 1.不管怎么样,先建立一个简单的MySQL数据表,如下所示 2.接下来要做的事情就是通过Mybatis对数据表进行 ...
- Apache Flink on K8s:四种运行模式,我该选择哪种?
1. 前言 Apache Flink 是一个分布式流处理引擎,它提供了丰富且易用的API来处理有状态的流处理应用,并且在支持容错的前提下,高效.大规模的运行此类应用.通过支持事件时间(event-ti ...
- PHP运行模式
1.运行模式 关于PHP目前比较常见的五大运行模式: 1)CGI(通用网关接口 / Common Gateway Interface) 2)FastCGI(常驻型CGI / Long-Live CGI ...
- php运行模式的比较(转)
PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli 命令行运行 ( ...
- 【转载】PHP运行模式的深入理解
PHP运行模式的深入理解 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-03我要评论 本篇文章是对PHP运行模式进行了详细的分析介绍,需要的朋友参考下 PHP运行模式有4钟:1) ...
- ARM处理器的寄存器,ARM与Thumb状态,7中运行模式 【转】
转自:http://blog.chinaunix.net/uid-28458801-id-3494646.html ARM处理器工作模式一共有 7 种 : USR 模式 正常用户模式,程序正常 ...
- PHP运行模式的深入理解
PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli 命令行运行 ( ...
随机推荐
- poj 3684 Physics Experiment 弹性碰撞
Physics Experiment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1489 Accepted: 509 ...
- week5 作业
week5 作业 1.描述GPT是什么,应该怎么使用? 描述GPT之前要简单了解MBR分区,MBR(Main Boot Record)叫做主引导记录,其位于磁盘的最前端,由一段代码组成,共占用512个 ...
- Thinkphp3.2.3中的RBAC权限验证
最近在用TP的RBAC权限控制,在这里记录学习一下.先来看看相关的概念 一.相关概念 访问控制与RBAC模型1.访问控制: 通常的多用户系统都会涉及到访问控制,所谓访问控制,是指通过某种 ...
- [CSP-S模拟测试]:爬(贪心)
题目传送门(内部题134) 输入格式 第一行两个数$N,L$. 接下来$N$行每行两个数$A_i,B_i$. 接下来$N$行每行一个整数$C_i$. 输出格式 一行一个整数表示答案,无解输出$-1$. ...
- 分布式-信息方式-ActiveMQ的Destination高级特性1
ActiveMQ的Destination高级特性 Destination高级特性----->Composite Destinations 组合队列Composite Destinations : ...
- JAVA-retry 重试
在看 ThreadPoolExecutor 源码时看到这么一段代码 retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c); // Ch ...
- String 类源码分析
String 源码分析 String 类代表字符序列,Java 中所有的字符串字面量都作为此类的实例. String 对象是不可变的,它们的值在创建之后就不能改变,因此 String 是线程安全的. ...
- VueLoaderPlugin作用
在webpack配置里加入new VueLoaderPlugin, 在plugin里打断点 然后debug: 在这个地方: 可以发现,在webpack初始化的阶段..webpack.js刚开始执行的时 ...
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_8 spring中bean的细节之生命周期
区分单例还是多例对象 单例的几个状态 初始化方法和销毁方法 设置成我们定义的方法 测试 有创建和初始化.但是没有销毁,.对象一直没有销毁的方法 main方法是一切应用程序的入门.当main方法结束后. ...
- RAC FAILover详解(转载)
Oracle RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会 ...