Spring声明式事务

Spring声明式事务

1 回顾事务

  • 把一组业务当成一个业务来做;要么都成功,要么都失败!
  • 事务在项目开发中,十分的重要,涉及到数据的一致性问题,不能马虎!
  • 确保完整性和一致性;

2 事务ACID原则

  • 原子性:事务的所有操作在数据库中要么全部正确反映出来,要么完全不反映
  • 一致性:事务要么都成功要么都失败,事务的资源和状态保持一致
  • 隔离性:多个业务操作同一个资源时要整资源不被破坏
  • 持久性:事务一旦提交就结果就会永久性的写入容器中不再被更改

3 spring中的事务管理

  • 声明式事务:AOP

  • 编程式事务:需要再代码中,进行事务的管理

    为什么需要事务?

    • 如果不配置事务,可能存在数据提交不一致的情况下;
    • 如果我们不在SPRING中去配置声明式事务,我们就需要在代码中手动配置事务!
    • 事务在项目的开发中十分重要,设计到数据的一致性和完整性问题,不容马虎!

4 声明式事务步骤

4.1 添加xml约束tx

在spring-dao上面添加xml约束tx

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

在下面写声明式事务的Bean

 <!--声明式事务!-->
    <bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
4.3 事务织入

结合AOP实现事务的织入

<!--结合AOP实现事务的织入!-->
    <!--配置事务通知!-->
    <tx:advice id="txAdvice" transaction-manager="transationManager">
        <!--给那些方法配置事务-->
        <!--配置事务的传播特性: new propagation = -->
        <tx:attributes>
            <tx:method name="addUser" propagation="REQUIRED"/>
            <tx:method name="deleteUser" propagation="REQUIRED"/>
            <tx:method name="updateUser" propagation="REQUIRED"/>
            <tx:method name="selectUser" propagation="REQUIRED"/>
            <tx:method name="queryUser" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

“*”表示所有的方法都支持事务

4.4 配置事务切入
<!--配置事务切入!-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.xzzlx.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
4.5 测试

在自己的测试类中实现CRUD测试业务

附上spring-dao.xml源代码:

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

    <!-- DataSource:使用Spring的数据源替换Mybatis的配置    c3p0 dbcp druid
    我们这里使用Spring提供的JDBC
    所以引入依赖我们引入了spring-jdbc  org.springframework.jdbc.datasource
    -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.18.176:3306/win_database01?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimeZone=Aisa/Shanghai"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory!-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--绑定Mybatis配置文件!-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/xzzlx/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate就是我们平时用的sqlSession!-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <!--声明式事务!-->
    <bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--结合AOP实现事务的织入!-->
    <!--配置事务通知!-->
    <tx:advice id="txAdvice" transaction-manager="transationManager">
        <!--给那些方法配置事务-->
        <!--配置事务的传播特性: new propagation = -->
        <tx:attributes>
            <tx:method name="addUser" propagation="REQUIRED"/>
            <tx:method name="deleteUser" propagation="REQUIRED"/>
            <tx:method name="updateUser" propagation="REQUIRED"/>
            <tx:method name="selectUser" propagation="REQUIRED"/>
            <tx:method name="queryUser" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务切入!-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.xzzlx.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>


</beans>

有问题可以私信问我,看到会及时回复的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值