Spring--Spring声明式事务

Spring

01: Spring简介
02: Spring–Spring组成及拓展
03: Spring–IoC理论推导&IoC本质
04: Spring–第一个Spring程序HelloSpring&修改上节案例
05: Spring–IoC创建对象方式
06: Spring–Spring配置
07: Spring–依赖注入(Dependency Injection)
08: Spring–Bean的作用域
09: Spring–自动装配Bean(使用自动装配xml配置)
10: Spring–使用注解实现自动装配(推荐使用)
11: Spring–注解开发
12: Spring–使用JavaConfig实现配置
13: Spring–静态代理再理解&AOP
14: Spring–AOP(通过 Spring API 实现&自定义类来实现Aop&使用注解实现)
15: Spring–回忆Mybatis
16: Spring–整合Mybatis
17: Spring–Spring声明式事务


1. 交由容器管理事务

在这里插入图片描述

只需修改配置文件即可
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <import resource="spring-dao.xml"/>

    <!--配置声明事务注入-->
    <!--要开启 Spring 的事务处理功能,在 Spring 的配置文件中创建一个 DataSourceTransactionManager 对象:-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
        <!--或者使用构造注入-->
        <!--<constructor-arg ref="dataSource" />-->
    </bean>

    <!--结合AOP实现事务的织入-->
    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给哪些方法配置事务-->
        <!--配置事务的传播特性 propagation
                PROPAGATION_REQUIRED:如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,这是最常见的选择。
                PROPAGATION_SUPPORTS:支持当前事务,如果没有当前事务,就以非事务方法执行。
                PROPAGATION_MANDATORY:使用当前事务,如果没有当前事务,就抛出异常。
                PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
                PROPAGATION_NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
                PROPAGATION_NEVER:以非事务方式执行操作,如果当前事务存在则抛出异常。
                PROPAGATION_NESTED:	如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED 类似的操作
        -->
        <tx:attributes>
            <!--            为增加方法添加事务-->
            <tx:method name="add" propagation="REQUIRED"/>
            <!--            为删除方法添加事务-->
            <tx:method name="delete" propagation="REQUIRED"/>
            <!--            为更新方法添加事务-->
            <tx:method name="update" propagation="REQUIRED"/>
            <!--            为查询方法添加事务-->
            <tx:method name="select" read-only="true"/>
            <!--            为全部方法配置事务-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务切入-->
    <aop:config>
        <!--该包下的所有方法-->
        <aop:pointcut id="txPointCut" expression="execution(* com.tian.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
</beans>

补充:需要导入的依赖坐标

        <!--       Spring事务管理支持包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.6</version>
        </dependency>
        <!--       aop植入包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeJiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值