栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1.xml 配置步骤

第一步、配置事务管理器

第二步、配置通知

第三步、配置切入点和切面

2.项目代码

bean1.xml配置文件



    
    

    
    
    
        
        
        
        
    

    
    
    
        
        
    

    
    
        
        
    

    
    
        
        
            
            
        
    

    
    
        
        
        
        
    


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();
    }
}


执行转账前

执行结果


可以看到事务开启成功, 当故障出现执行了回滚, 保证了数据的正确性.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489038.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号