• 【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的确实没有加进去
  • 相关阅读:
    【高级篇】Java JVM实战 之 内存调优
    FAST协议详解4 存在图PMap
    排序算法(一)
    Open3D(C++) 深度图像与彩色图像转三维点云
    java中泛型的理解
    解决mysql去掉字段空格:中间空格,左侧空格,右侧空格,两端空格,水平制表符(tab键或者\t)空格,换行键(\n)空格,回车键(Enter键)空格
    echarts设置竖向不同区间范围颜色,并且x轴自定义轴线刻度范围
    强化学习-学习笔记5 | AlphaGo
    力诺转债上市价格预测
    java面试题之mybatis篇
  • 原文地址:https://blog.csdn.net/weixin_40986490/article/details/128190700