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

02-Spring中的事务

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

02-Spring中的事务

文章目录
      • 1. Spring中的事务
        • 1.1事务回顾
        • 1.2 Spring通过配置XML方式进行事务管理
          • 1.2.1 建表
          • 1.2.2 pom中导入依赖
          • 1.2.3 AccountController
          • 1.2.4 AccountService和AccountServiceImpl
            • AccountService
            • AccountServiceImpl
          • 1.2.5 AccountMapper和accountMapper.xml
            • AccountMapper
            • accountMapper.xml
          • 1.2.6 spring配置文件applicationContext.xml
          • 5.6.7 注解方式的spring配置文件
        • 1.2 测试类
        • 1.3 配置AOP
      • 源码仓库

源码见文章结尾

1. Spring中的事务 1.1事务回顾

事务是逻辑上的一组操作,要么全都成功,要么全部失败
事务的特性:ACID

原子性:事务不可分割
一致性:事务执行前后,数据完整新保持一致
隔离性:一个事务执行的时候,不应该受到其他事务的打扰
持久性:一旦结束,数据就会永久的保存到数据库

如果不考虑隔离性:

  • 脏读:一个事务读取到另一个事务未提交的数据
  • 不可重复读:一个事务读取到另一个事务已近提交的数据(update),导致没有刷新的情况下导致查询结果不一致
  • 虚读:一个事务读取到另一个事务已经提交的数据(insert)导致另一个事务多次查询结果不一致

事务的隔离级别:

  • 未提交读:以上情况都有可能发生。
  • 已提交读:避免脏读,但不可重复读,虚读是有可能发生。
  • 可重复读:避免脏读,不可重复读,但是虚读有可能发生。
    串行的:避免以上所有情况.
1.2 Spring通过配置XML方式进行事务管理 1.2.1 建表
CREATE TABLE `t_account` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

INSERT INTO `t_account` VALUES ('1', 'Tom', '1000');
INSERT INTO `t_account` VALUES ('2', 'Jerry', '1000');
1.2.2 pom中导入依赖
  
            org.springframework
            spring-context
            5.3.22
        
        
        
            org.springframework
            spring-jdbc
            5.3.22
        
        
        
            org.aspectj
            aspectjweaver
            1.9.9.1
        

        
            org.springframework
            spring-tx
            5.3.22
        

        
        
            org.mybatis
            mybatis
            3.5.9
        
        
        
            org.mybatis
            mybatis-spring
            2.0.7
        
        
        
            mysql
            mysql-connector-java
            8.0.29
        
        
        
            com.alibaba
            druid
            1.2.9
        

        
            junit
            junit
            4.13.2
            test
        

        
            log4j
            log4j
            1.2.12
        

        
            org.springframework
            spring-aop
            5.3.22
        
1.2.3 AccountController
package com.dzh.controller;

import com.dzh.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


@Controller
public class AccountController {

    private AccountService accountService;

    
    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    
    public void transfer(Integer fromAccount, Integer toAccount, Double money) {
        accountService.transfer(fromAccount,toAccount,money);
    }

}

1.2.4 AccountService和AccountServiceImpl
  • AccountService
package com.dzh.service;


public interface AccountService {

    void transfer(Integer fromAccount, Integer toAccount, Double money);
}

  • AccountServiceImpl
package com.dzh.service.impl;

import com.dzh.mapper.AccountMapper;
import com.dzh.service.AccountService;
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
@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {
    
    private AccountMapper accountMapper;

    
    @Autowired
    public void setAccountMapper(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Override
    public void transfer(Integer fromAccount, Integer toAccount, Double money) {
        
        accountMapper.transferOut(fromAccount,money);
        //设置异常测试
        int i = 1/0;
        
        
        accountMapper.transferIn(toAccount,money);

    }
}
1.2.5 AccountMapper和accountMapper.xml
  • AccountMapper
package com.dzh.mapper;

import org.apache.ibatis.annotations.Param;


public interface AccountMapper {
    
    void transferOut(@Param("fromAccountId") Integer fromAccount, @Param("money") Double money);

    void transferIn(@Param("toAccountId") Integer toAccount, @Param("money") Double money);

}
  • accountMapper.xml





    
    
        update t_account set money = money - #{money} where id = #{fromAccountId}
    

    
    
        update t_account set money = money + #{money} where id = #{toAccountId}
    


1.2.6 spring配置文件applicationContext.xml



    
    


    


    
        
        
        
        
    

    
        
        


        

        ClassPathXmlApplicationContext ac =
                new ClassPathXmlApplicationContext("bean.xml");
        AccountController accountController = (AccountController) ac.getBean("accountController");

        accountController.transfer(2,1,1000d);

    }
}

1.3 配置AOP
package com.dzh.advice;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class AccountAdvice {

    
    @Pointcut("execution(public void com.dzh.service.impl.AccountServiceImpl.transfer())")
    public void pc(){}

    

    @Before("AccountAdvice.pc()")
    public void before() {
        System.out.println("--开始--");
    }

    
//    @After("execution(public void com.dzh.service.impl.UserServiceImpl.addUser())")
//    @After("AccountAdvice.pc()")
    public void after() {
        System.out.println("----");
    }

    
//    @AfterReturning("AccountAdvice.pc()")
    public void after_returning() {
        System.out.println("转账成功!");
    }

    
//    @AfterThrowing("AccountAdvice.pc()")
    public void after_throwing() {
        System.out.println("转账失败");
    }

    

//    @Around("AccountAdvice.pc()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        System.out.println("开始转账");
        System.out.println("......");
        proceedingJoinPoint.proceed();
        System.out.println("转账成功");

    }
}

源码仓库
https://github.com/dongzihaoa/spring_tx.git
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031300.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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