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 命令行运行 ( ...
随机推荐
- Word:转换PDF
本文适用于Word 2007 + Windows 7,造冰箱的大熊猫@cnblogs 2018/8/3 一.Word文档转PDF文档 把Word文档转换为PDF,有两个免费解决方案 1.Microso ...
- Scratch的入门笔记
最近发现人工智能和编程在小学开始普及,由于好奇,所以开始去了解儿童编程方面的知识,希望增加一些儿童编程教育相关的知识面,跟上发展潮流. Scratch是一款由麻省理工学院的“终身幼儿园团队”(Life ...
- 如何在matalb图像上添加公式符号
方法: legend({'$\sigma(t)$'},'interpreter','latex') 效果如下:
- getFieldDecorator用法(一)——登录表单
之前使用antd的ui表单,却没发现这么好用的用法,推荐给大家 import React from "react"; import { Card, Form, Input, But ...
- apply,call,bind函数作用与用法
作用 可以把方法借给其它对象使用,并且改变this的指向 a.apply(b,[3,2]);//this指向由a变为b, a的方法借给b使用 实例: function add(a,b){ ...
- 【学习】mysql 时间戳与日期格式的相互转换
1.UNIX时间戳转换为日期用函数: FROM_UNIXTIME() ); 输出:2006-08-22 12:11:10 2.日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() Sel ...
- jpa hibernate mybatis
jpa: entityManagerFactory: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean hibern ...
- 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/ 文章目录 [隐 ...
- DB2日常管理
执行时间最长的10条SQL语句(按时间降序排列),可保存为脚本方便调用:db2 "SELECT rows_read / (num_executions + 1) as avg_rows_re ...
- 百度地图api根据地址获取经纬度
package com.haiyisoft.cAssistant;import java.io.BufferedReader;import java.io.IOException; import ja ...