• 【Spring】事务管理


    项目目录

    在这里插入图片描述

    • UserMapper.xml写sql语句
    • UserMapperImpl继承sqlSessionDaoSupport然后在里面做一个addUser和一个delete
    • 我们delete语句故意写错
      在这里插入图片描述
      在这里插入图片描述
    • 但是我们想把add和delete当成一个事务
    • delete错了,add也不应该成功
    • 所以我们需要进行事务管理配置

    spring-dao.xml

    
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            https://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        
        <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://47.96.75.130:3306/spring_test?userSSL=true&
                    userUnicode=true&characterEncoding=UTF-8"/>
            <property name="username" value="lyh"/>
            <property name="password" value="abAB12"/>
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="datasource" />
            
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:com/mapper/*.xml"/>
        bean>
    
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <constructor-arg ref="datasource" />
        bean>
    
        
        
        <tx:advice id="tx1" transaction-manager="transactionManager">
            
            
            <tx:attributes>
                <tx:method name="add" propagation="REQUIRED"/>
                <tx:method name="delete" propagation="REQUIRED"/>
                <tx:method name="update" propagation="REQUIRED"/>
                <tx:method name="*" propagation="REQUIRED"/>
                <tx:method name="query" read-only="true"/>
            tx:attributes>
        tx:advice>
    
        
        <aop:config>
            <aop:pointcut id="txpointxut" expression="execution(* com.mapper.*.*(..))"/>
            <aop:advisor advice-ref="tx1" pointcut-ref="txpointxut"/>
        aop:config>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 三步走
    • 头文件加入tx等
    • 申明式事务bean
    • 配置事务管理器advice,包括给哪些方法配置事务,以及事务的特性
    • 配置事务切入,在哪个切入点切入

    测试

    在这里插入图片描述

    • 可以发现id为6的确实没有加进去
  • 相关阅读:
    Altium Designer 22 修改选中元件的层属性
    YOLOv5实现车辆检测(含车辆检测数据集+训练代码)
    每日提醒按时完成各项任务的手机app有什么?
    vue当中的mixin混入、插件使用
    Webpack5 搭建Vue项目(进阶版)
    前端面试开发-js代码实现篇
    Jmeter响应时间和tps监听器使用教程
    NPM配置国内镜像源
    R语言使用dplyr包的transmute函数计算dataframe数据中的指定数据列的移动窗口均值
    思科 Packet Tracer实验(一)
  • 原文地址:https://blog.csdn.net/weixin_40986490/article/details/128190700