13、声明式事务

13.1、回顾事务

  • 把一组业务当成一个业务来做; 要么都成功要么都失败!
  • 事务在开发中,十分的重要,涉及到数据的一致性问题, 不得马虎!
  • 确保完整性和一致性
  • 分享个踩的坑,MyISAM不支持事务,InnoDB可以

事务的ACID原则:

  • 原子性
  • 一致性
  • 持久性
    • 事务一旦提交, 无论系统发生什么问题, 结果都不会再被影响, 被持久化的写到存储器中
  • 隔离性(幻读,脏读,不可重复读)
    • 多个业务可能操作同一个资源防止数据损坏



13.2、spring中的事务管理

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

![image.png](https://img-blog.csdnimg.cn/img_convert/a5b4b6db4d39e86e23e093274fd02c6a.png#clientId=u0e2c20bd-f91b-4&from=paste&height=399&id=u169d3297&margin=[object Object]&name=image.png&originHeight=399&originWidth=788&originalType=binary&ratio=1&size=227739&status=done&style=none&taskId=u91710070-abb8-4cbb-b2c9-491fa4a6a61&width=788)
步骤: Spring-11-transaction
①配置Spring环境

<?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:tx="http://www.springframework.org/schema/tx"
       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/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

②配置事务

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


    <!--2、结合AOP实现事务的织入-->

    <!--2.1、配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给那些方法配置事务-->
        <tx:attributes>
            <!--配置事务的传播特性(有默认值REQUIRED,所以不用写)-->
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="query" read-only="true"/>
            <tx:method name="*"/><!--所有方法-->
        </tx:attributes>
    </tx:advice>

    <!--2.2、配置事务切入-->
    <aop:config>
        <!--该包下的所有文件(xml)以及所包含的方法都切入-->
        <aop:pointcut id="txPointCut" expression="execution(* com.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

完整的Spring.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:tx="http://www.springframework.org/schema/tx"
       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/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--DataSource: 使用Spring的数据源替代了mybatis-config.xml的配置<environments> c3p0 dbcp druid -->
    <!--我这里使用的是Spring提供的JDBC : org.springframework.jdbc.datasource.DriverManagerDataSource
    所以一定要导spring-jdbc这个包-->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory 可以代替原来的mybatis-config.xml的所有工作!!
                          可以代替Mybatis中的工具类(创建sqlSessionFactory)-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean ">
        <!--配置环境-->
        <property name="dataSource" ref="datasource"/>

        <!--绑定Mybatis核心配置文件
        (完全可以不加这一步,但建议绑定,
        因为类型别名和设置建议在mybatis-config.xml中设置)-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>

        <!--注册映射器 <mappers>-->
        <property name="mapperLocations" value="classpath:com/mapper/*.xml"/><!--匹配该包下的所用xml文件-->
    </bean>

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

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


    <!--2、结合AOP实现事务的织入-->

    <!--2.1、配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给那些方法配置事务-->
        <tx:attributes>
            <!--配置事务的传播特性(有默认值REQUIRED,所以不用写)-->
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="query" read-only="true"/>
            <tx:method name="*"/><!--所有方法-->
        </tx:attributes>
    </tx:advice>

    <!--2.2、配置事务切入-->
    <aop:config>
        <!--该包下的所有文件(xml)以及所包含的方法都切入-->
        <aop:pointcut id="txPointCut" expression="execution(* com.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值