18-Spring5 基于xml配置文件开启事务管理

1.xml 配置步骤

第一步、配置事务管理器
在这里插入图片描述

第二步、配置通知
18-Spring5 基于xml配置文件开启事务管理_第1张图片

第三步、配置切入点和切面
18-Spring5 基于xml配置文件开启事务管理_第2张图片

2.项目代码

18-Spring5 基于xml配置文件开启事务管理_第3张图片

bean1.xml配置文件


<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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <context:component-scan base-package="com.limi">context:component-scan>

    
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/db_springtest" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
        <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>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <tx:advice id="txadvice">
        
        <tx:attributes>
            
            <tx:method name="change" propagation="REQUIRED"/>
        tx:attributes>
    tx:advice>

    
    <aop:config>
        
        <aop:pointcut id="pc" expression="execution(* com.limi.service.AccountService.*(..))"/>
        
        <aop:advisor advice-ref="txadvice" pointcut-ref="pc"/>
    aop:config>

beans>

Account

package com.limi.entity;

public class Account {
     
    private Integer id;

    private String userName;

    private Double price;

    public Account(){
     }

    public Account(Integer id, String userName, Double price) {
     
        this.id = id;
        this.userName = userName;
        this.price = price;
    }

    public Integer getId() {
     
        return id;
    }

    public void setId(Integer id) {
     
        this.id = id;
    }

    public String getUserName() {
     
        return userName;
    }

    public void setUserName(String userName) {
     
        this.userName = userName;
    }

    public Double getPrice() {
     
        return price;
    }

    public void setPrice(Double price) {
     
        this.price = price;
    }
}

AccountDao

package com.limi.dao;

public interface AccountDao {
     

    //修改账户余额
    int updateMoneyById(Integer id, Double money);
}

AccountDaoImpl

package com.limi.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements  AccountDao{
     

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int updateMoneyById(Integer id, Double money) {
     

        String sql;
        if(money>0)
            sql = "update t_account set money = money+? where id = ?";//加法
        else
        {
     
            money = -money;
            sql = "update t_account set money = money-? where id = ?";//减法
        }
        int res = jdbcTemplate.update(sql, money, id);
        return res;
    }
}

AccountService

package com.limi.service;

import com.limi.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountService {
     

    @Autowired
    private AccountDao accountDao;

    public void change(){
     
        //模拟1号账户给2号账户转账200
        accountDao.updateMoneyById(1, -200.00);
        //模拟出故障
        int a = 10/0;
        accountDao.updateMoneyById(2, 200.00);
    }
}

测试类MyTest

package com.limi.test;
import com.limi.dao.AccountDao;
import com.limi.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
     

    @Test
    public void test1(){
     
        //1.加载bean的xml文件, 以src为根目录
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        //2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.class
        AccountService accountService = context.getBean("accountService", AccountService.class);

        //3.使用对象
        accountService.change();
    }
}


执行转账前
18-Spring5 基于xml配置文件开启事务管理_第4张图片
18-Spring5 基于xml配置文件开启事务管理_第5张图片

执行结果
18-Spring5 基于xml配置文件开启事务管理_第6张图片
18-Spring5 基于xml配置文件开启事务管理_第7张图片
可以看到事务开启成功, 当故障出现执行了回滚, 保证了数据的正确性.

你可能感兴趣的:(Spring5,xml,java,服务器)