@Transactional管理事务

使用基于@Transactional的Spring配置

<beans:bean id="transactionManager"  class="org.springframework.orm.jpa.JpaTransactionManager">  
    <beans:property name="dataSource" ref="dataSource" />  
    <beans:property name="entityManagerFactory" ref="entityManagerFactory" />  
</beans:bean>  
  
<!-- 声明使用注解式事务 -->  
<tx:annotation-driven transaction-manager="transactionManager" />  
直接在需要事务的类或者方法上使用@Transactional就可以标记该类或者该方法使用事务管理数据库操作

注意加载顺序,spring-mybatis和spring-mvc加载的顺序,在配置文件中的顺序如下

spring-mvc.xml文件,不加载controller文件
<!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。   -->  
 <context:component-scan base-package="com.sence">  
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />   
 </context:component-scan>  
 
spring-mybatis.xml文件,不加载service
<!-- 扫描所有的controller 但是不扫描service-->  
<context:component-scan base-package="com.sence">  
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
</context:component-scan>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值