mybatis使用的一点小结:session运行模式及批量提交
2012-12-05 16:28
573 查看
mybatis的执行器有三种类型:
ExecutorType.SIMPLE
这个类型不做特殊的事情,它只为每个语句创建一个PreparedStatement。
ExecutorType.REUSE
这种类型将重复使用PreparedStatements。
ExecutorType.BATCH
这个类型批量更新,且必要地区别开其中的select 语句,确保动作易于理解。
可以在配置sqlSession时指定相应的类型:
也可以在通过SqlSessionFactory创建一个SqlSession时指定:
openSession有很多方式:
默认执行器是SIMPLE。
三种类型执行器除了上面的特点外,在使用过程中还发现:
ExecutorType.SIMPLE:可以返回自增键,只需要在mapper文件中,增加属性: useGeneratedKeys="true" keyProperty="productId"
那么自增键会在事务提交后,自动设置到传入的user对象中
ExecutorType.BATCH:当前最新版本的mybatis(mybatis-3.2.0)无法再返回自增键值,只返回最后一个更新记录的自增键值(基本上没上意义)。并且无法返回更新数据的记录数
要实现批量插入数据有两种方式:
使用SIMPLE执行器,借助foreach动态sql语句,使用Insert values(...),(...),(...) 的方式,这种方式无法取到自增键
比如
使用BATCH执行器,但是SqlSession的执行器类型一旦设置就无法动态修改,所以如果在配置文件中设置了执行器为SIMPLE,当要使用BATCH执行器时,需要临时获取:
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(); }这个方法仍然需要包在事务中
相关文章推荐
- session运行模式、批量提交
- mybatis 使用经验小结,batch批量提交大量数据
- mybatis 使用经验小结,batch批量提交大量数据
- mybatis 使用经验小结,batch批量提交大量数据
- mybatis+tk mapper使用oracle sequence自增时其中一点小结
- 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
- Mybatis-Plus 搭建与使用入门(小结)
- JavaWeb学习总结(十三)——使用Session防止表单重复提交
- 使用mybatis 实现批量插入,主键自增长
- 设置开发(Debug模式)与提交(Release模式)的运行环境以及如何屏蔽NSLog
- 使用服务器端运行模式实现一个Grid
- 防止页面重复提交(使用UUID--Session)
- mybatis在使用insert into 批量插入数据的问题
- JavaWeb学习总结(十三)——使用Session防止表单重复提交
- .net的session详解 存储模式 存到数据库中 使用范围与大小限制 生命周期
- mybatis SqlSessionDaoSupport的使用(附代码下载)(转)
- myBatis使用全注解小结
- 使用Log4jdbc-log4j2监听MyBatis中运行的SQL和Connection
- mybatis实战教程(mybatis in action)之十:mybatis SqlSessionSupport 的使用,构件DAO 层的应用
- MapReduce程序的3种集群提交运行模式详解---基于Windows与Linux两种开发环境