Spring5框架——通过配置文件(XML文件)对类或方法添加事务操作

44b64366391a4d61b5d9ba5fad0935b4.jpg

 

 (开心娱乐一下)

前面我们讲解了通过注解方法对类或方法添加事务,式它们可以被执行时不会因为一些异常导致数据产生问题,在我们的开发过程中注解方式是最常用的,但是配置文件也需要进行掌握知道如何通过配置文件方式实现事务操作,上一篇文章讲解了事务操作的一些参数,如果没有了解可以去我主页Spring专栏了解,因为下面我们将通过配置方式再次实现那些事务参数。

 

之前我们通过注解方式,解决银行转账业务实现:用户一向用户二转账,但是由于一些异常问题导致用户一数据库中的money值减少了,但是用户二的money值却没有增加,同样我们本篇文章也是以此为例,通过配置文件方式实现异常回滚。

案例准备:

1:在MySQL中创建一个表,模拟银行后台数据库

62ae046dab6141fc8cf3e47c8ffc7368.png

2.创建类实现转账业务

@Service
public class operat_system {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void changmoney(Object[] user,int id){  //实现银行取钱和存钱功能,在一表中的money值发生变化
        if (id==1){
            String sql ="UPDATE myemployees.`bank` SET myemployees.`bank`.`money`=myemployees.`bank`.`money`-? WHERE myemployees.`bank`.`id`=?";
            jdbcTemplate.update(sql,user);
            System.out.println("取钱成功");
        }
        else{
            String sql ="UPDATE myemployees.`bank` SET myemployees.`bank`.`money`=myemployees.`bank`.`money`+? WHERE myemployees.`bank`.`id`=?";
            jdbcTemplate.update(sql,user);
            System.out.println("存钱成功");
        }
    }

    public void zz(operat_system aa,Object[] user,Object[] user2){   //实现银行用户的转账功能,从用户一转到用户二
        aa.changmoney(user,1);
        int m = 10/0;              //模拟异常出现
        aa.changmoney(user2,0);
        System.out.println("转账成功");
    }
}

 3.配置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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql://127.0.0.1:3306" />
        <property name="username" value="demo1" />
        <property name="password" value="123" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>

    <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <context:component-scan base-package="new_study.bank_system"></context:component-scan>
    <!--创建事务管理器-->
    <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置通知,对方法添加事务操作,并添加到事务管理器-->
    <tx:advice id="txadv" transaction-manager="tm">
        <tx:attributes>
<!--            为zz方法添加事务传播行为-->
            <tx:method name="zz" propagation="REQUIRED"/>
<!--            为zz方法添加事务隔离-->
            <tx:method name="zz" isolation="SERIALIZABLE"></tx:method>
<!--            为zz方法设置事务超时时间-->
            <tx:method name="zz" timeout="-1"></tx:method>
<!--            为zz方法设置只读操作-->
            <tx:method name="zz" read-only="false"></tx:method>
        </tx:attributes>
    </tx:advice>
<!--    配置切入点和切面-->
    <aop:config>
<!--        配置切入点,*(..)是指operat_system类中的所有方法-->
        <aop:pointcut id="point" expression="execution(* new_study.bank_system.operat_system.*(..))"/>
<!--        配置切面-->
        <aop:advisor advice-ref="txadv" pointcut-ref="point"></aop:advisor>
    </aop:config>

</beans>

4:测试类:

package new_study.bank_system;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("new_study/bank_system/bean2.xml");
    operat_system bank1 = context.getBean("operat_system", operat_system.class);
      Object[] a1 = {100, "1"};
      Object[] a2 = {100, "2"};
    }
}

输出结果:

438be77aecf14a818207122e56aa1d86.png

只执行了第一个方法,然后遇见了异常

看一下数据库中的数据

945cdd696cde46fda3b04ede8c1f7628.png 

 执行了第一个方法,但是数据库中的数据并没有发生改变,说明操作进行了回滚

以上就是配置文件配置的事务操作

后期会不断地更新关于Spring5框架的知识

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不想睡醒的梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值