Mybatis事务详细配置 (SqlSessionDaoSupport方式, 已经经过验证 )

 1.       applicationContext.xml

[html]  view plain copy
  1. <!--这是原先的sqlSession和dao的配置, 增加事务时候配置保持不变-->  
[html]  view plain copy
  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  2.         <property name="dataSource" ref="dataSource"></property>  
  3.         <property name="configLocation" value="classpath:mybatis.xml"></property>  
  4.         <property name="mapperLocations" value="classpath:com/sjl/dao/*-mapper.xml"></property>  
  5.     </bean>  
  6.       
  7.     <bean id="idao" class="com.sjl.dao.IdaoImpl">  
  8.         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
  9.     </bean>          
 

 

[html]  view plain copy
  1.    <!--增加的mybatis 事务控制 -->  
  2.    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
  3.          <property name="dataSource" ref="dataSource"></property>      
  4.    </bean>   
  5. <tx:annotation-driven transaction-manager="transactionManager"/>  
   2.     然后在dao中使用 @Transactional 注解声明事务

[java]  view plain copy
  1. package com.sjl.base;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import javax.annotation.Resource;  
  8.   
  9. import org.springframework.transaction.annotation.Transactional;  
  10.   
  11. import com.sjl.common.EntityClassUtil;  
  12. import com.sjl.common.Pager;  
  13. import com.sjl.dao.Idao;  
  14.   
  15. public class AbstractBaseDao<T, PK extends Serializable> implements BaseDao <T, PK> {  
  16.     @Resource  
  17.     private Idao<T, PK> idao;  
  18.       
  19.     private Class entityClass = EntityClassUtil.getEntityClass(getClass());  
  20.  /**
      * 如果没有配置@Transactional    使用sqlSession的jdbc事务  每一个操作都是一个单独的事务
      * 抛出异常事务也不会回滚   无论抛出什么异常了
      */
  21.     @Transactional</span>  
  22.     public void save(T entity) {  
  23.           
  24.         idao.save(entity);  
  25.  /**
  26.   * 只有抛出运行期异常  事务才会回滚,如果抛出Exception默认是不会回滚的,
  27.   * 需要配置@Transactional注解才可以  
  28.  */
  29.         throw new RuntimeException();//抛出异常, 事务回滚  
  30.   
  31.     }  
  32.     public void delete(PK pk) {  
  33.         idao.delete(entityClass, pk);  
  34.           
  35.     }  
  36.     public Pager<T> findByPage(int pageOffset, int pageSize) {  
  37.           
  38.         HashMap<String, Integer> map = new HashMap();  
  39.         map.put("pageOffset", pageOffset);  
  40.         map.put("pageSize", pageSize);  
  41.         Pager<T> pager = idao.findByPage(entityClass, map);  
  42.         return pager;  
  43.           
  44.           
  45.     }  
  46.   
  47.       
  48. }  
[java]  view plain copy
  1. 其中的idao定义如下:  
  2. package com.sjl.dao;  
  3.   
  4. import java.io.Serializable;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.mybatis.spring.support.SqlSessionDaoSupport;  
  9. import org.springframework.stereotype.Repository;  
  10.   
  11. import com.sjl.common.Pager;  
  12.   
  13. @Repository("idao")  
  14. public class IdaoImpl<T, PK extends Serializable> extends SqlSessionDaoSupport implements Idao<T, PK>  {  
  15.   
  16.     public void save(T entity) {  
  17.           
  18.         this.getSqlSession().insert(entity.getClass().getName()+".add", entity);  
  19.     }  
  20.   
  21.     public void delete(Class<T> entityClass, PK pk) {  
  22.         this.getSqlSession().insert(entityClass.getName()+".delete", pk);  
  23.           
  24.     }  
  25.   
  26.     public Pager<T> findByPage(Class<T> entityClass, Map param){  
  27.           
  28.         Pager<T> pager = new Pager();  
  29.           
  30.         List<T> pageContent = this.getSqlSession().selectList(entityClass.getName()+".findPageContent", param);  
  31.         pager.setPageContent(pageContent);  
  32.           
  33.         int count = this.getSqlSession().selectOne(entityClass.getName()+".findTotal");  
  34.         pager.setTotalCount(count);  
  35.           
  36.         return pager;  
  37.           
  38.     }  
  39. }  
以上通过@Transactional 注解配置事务,网上还有通过aop元素配置事务的方式, 暂不作研究。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值