mybatis使用的一点小结:session运行模式及批量提交(转)

mybatis的执行器有三种类型:

 

  • ExecutorType.SIMPLE
这个类型不做特殊的事情,它只为每个语句创建一个PreparedStatement。
  • ExecutorType.REUSE
这种类型将重复使用PreparedStatements。
  • ExecutorType.BATCH
这个类型批量更新,且必要地区别开其中的select 语句,确保动作易于理解。

 

 

可以在配置sqlSession时指定相应的类型:

 

[html]  view plain  copy
 
  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时指定:

 

[html]  view plain  copy
 
  1. sqlSessionFactory.openSession(ExecutorType.BATCH);  

 

 

openSession有很多方式:

 

[html]  view plain  copy
 
  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"
    [html]  view plain  copy
     
    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(...),(...),(...) 的方式,这种方式无法取到自增键

    比如
    [html]  view plain  copy
     
    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执行器时,需要临时获取:
    [java]  view plain  copy
     
    1. SqlSession session = sqlSessionTemplate.getSqlSessionFactory()  
    2.                .openSession(ExecutorType.BATCH, false);  
    3.        try {  
    4.            UserDao batchUserDao = session.getMapper(UserDao.class);  
    5.   
    6.            for (UserCommand user : users) {  
    7.                batchUserDao.insertUser(user);  
    8.            }  
    9.            session.commit();  
    10.            // 清理缓存,防止溢出  
    11.            session.clearCache();  
    12.   
    13.            // 添加位置信息  
    14.            userLbsDao.insertUserLbses(users);  
    15.   
    16.        } finally {  
    17.            session.close();  
    18.        }  
    这个方法仍然需要包在事务中
 
 转自:http://blog.csdn.net/meiwen1111/article/details/8260387
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值