Spring学习笔记(十五)Spring事务XML版-推荐

在XML版中完成注解@Transactional的功能

源码获取github

1.项目结构,数据库表跟注解版一样

2.XML配置

<!--5.配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="druidDataSource"/>
</bean>

<!--6.告知哪些方法是需要被事务管理器进行管理的,
        等价于注解版,在方法上面写@Transactional
    -->
<tx:advice transaction-manager="transactionManager" id="serviceAdvice">
   <tx:attributes>
      <!--哪些方法,也可以设置属性,对应注解版的属性-->
      <tx:method name="get*" read-only="true"/>
      <tx:method name="load*" read-only="true"/>
      <tx:method name="list*" read-only="true"/>
      <tx:method name="find*" read-only="true"/>
      <tx:method name="query*" read-only="true"/>
      <tx:method name="sel*" read-only="true"/>
      <tx:method name="check*" read-only="true"/>
      <tx:method name="valid*" read-only="true"/>
      <tx:method name="login*" read-only="true"/>
      <!--这是方法都是查询的方法,设置为只读-->

      <tx:method name="*" propagation="REQUIRED"/>
   </tx:attributes>
</tx:advice>

<!--7.配置AOP和tx:advice配合使用,告知我哪个层次下的方法会被事务管理器进行管理-->
<aop:config proxy-target-class="true">
   <aop:pointcut id="servicePointCut" expression="execution(* com.hs..service.*Service.*(..))"/>
   <aop:advisor advice-ref="serviceAdvice" pointcut-ref="servicePointCut"/>
</aop:config>
<!--8.启动事务注解:告知该方法是事务方法(一个错,其他全部错),而不是普通方法
 transaction-manager="transactionManager"可以省略
-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

意思就是:我监控的是com.hs..service.*Service:service层下以Service结尾的类下的:这些方法

 <tx:method name="get" read-only="true"/>
      <tx:method name="load" read-only="true"/>
      <tx:method name="list" read-only="true"/>
      <tx:method name="find" read-only="true"/>
      <tx:method name="query" read-only="true"/>
      <tx:method name="sel" read-only="true"/>
      <tx:method name="check" read-only="true"/>
      <tx:method name="valid" read-only="true"/>
      <tx:method name="login" read-only="true"/>
      <!--这是方法都是查询的方法,设置为只读-->
<tx:method name="*" propagation="REQUIRED"/>

3.其他代码跟注解版一样,只是去掉@Transactional

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值