系列八、编程式事务

一、概述

        编程式事务是指程序员手动的在业务代码中控制事务执行的流程,业务方法正常执行提交事务,业务方法执行过程中出现异常则回滚事务。

二、编程式事务环境搭建

2.1、项目概览

系列八、编程式事务_第1张图片

2.2、pom.xml


	
	
		org.springframework
		spring-aop
		5.2.5.RELEASE
	
	
		org.springframework
		spring-beans
		5.2.5.RELEASE
	
	
		org.springframework
		spring-context
		5.2.5.RELEASE
	
	
		org.springframework
		spring-core
		5.2.5.RELEASE
	
	
		org.springframework
		spring-expression
		5.2.5.RELEASE
	
	
		org.springframework
		spring-test
		5.2.5.RELEASE
	

	
	
		mysql
		mysql-connector-java
		8.0.26
	
	
		com.alibaba
		druid
		1.2.16
	
	
		org.springframework
		spring-jdbc
		5.3.27
	
	
		org.mybatis
		mybatis
		3.5.11
	
	
		org.mybatis
		mybatis-spring
		2.1.0
	

	
	
		org.projectlombok
		lombok
		1.18.22
	
	
		org.slf4j
		slf4j-api
		1.7.32
	
	
		ch.qos.logback
		logback-classic
		1.2.10
	

	
	
		cglib
		cglib
		3.1
	
	
		aopalliance
		aopalliance
		1.0
	
	
		org.aspectj
		aspectjweaver
		1.9.19
	

	
	
		junit
		junit
		4.13.2
		test
	
	
		com.alibaba
		fastjson
		1.2.76
	
	
		org.apache.commons
		commons-collections4
		4.3
	
	
		org.apache.commons
		commons-lang3
		3.11
	
	
		cn.hutool
		hutool-all
		5.7.22
	
	
		com.fasterxml.jackson.core
		jackson-databind
		2.12.1
	
	
		commons-logging
		commons-logging
		1.1.1
	

2.3、applicationContext.xml




    
    

    
    
    
        
        
        
        
    
    
    
        
        
        
        
            
                
            
        
    
    
    
        
        
    

    
    
        
    

2.4、db.properties

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/20230828_spring5?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
db.username=root
db.password=123456

2.5、AccountDO.java

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/24 8:22
 * @Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
public class AccountDO implements Serializable {
    /**
     * 主键
     */
    private Integer id;

    /**
     * 银行卡号
     */
    private String accountNo;

    /**
     * 账户余额
     */
    private BigDecimal amount;

}

2.6、AccountMapper.java

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/24 8:23
 * @Description:
 */
public interface AccountMapper {

    /**
     * 账户入账
     * @param accountNo
     * @param amount
     * @return
     */
    Integer accountEntry(@Param("accountNo") String accountNo, @Param("amount") int amount);


    /**
     * 账户支出
     * @param accountNo
     * @param amount
     * @return
     */
    Integer accountExpenditure(@Param("accountNo") String accountNo, @Param("amount") int amount);

}

2.7、AccountMapper.xml





    
        update account set amount = amount + #{amount} where account_no = #{accountNo}
    

    
        update account set amount = amount - #{amount} where account_no = #{accountNo}
    

2.8、AccountService.java

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/24 8:25
 * @Description:
 */
public interface AccountService {

    /**
     * 转账
     */
    void transferMoney();

}

2.9、AccountServiceImpl.java

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/24 8:25
 * @Description:
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private DataSourceTransactionManager transactionManager;

    /**
     * 转账
     *
     * @return
     */
    @Override
    public void transferMoney() {
        TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);

        try {
            // Jack 转出100元
            accountMapper.accountExpenditure("Jack", 100);

            // 模拟异常
            int i = 10 /0;

            // Rose 入账100元
            accountMapper.accountEntry("Rose", 100);

            // 提交事务
            transactionManager.commit(transactionStatus);
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            transactionManager.rollback(transactionStatus);
        }
    }
}

2.10、SpringJunitTest.java

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/23 19:12
 * @Description: Spring整合单元测试
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringJunitTest {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private AccountService accountService;

    /**
     * 入账 & 出账
     */
    @Test
    public void accountEntryTest() {
        Integer entryResult = accountMapper.accountEntry("Jack", 100);
        Integer roseResult = accountMapper.accountExpenditure("Rose", 100);

        log.info("entryResult:{},roseResult:{}",entryResult,roseResult);
    }

    /**
     * 转账
     */
    @Test
    public void transferMoneyTest() {
        accountService.transferMoney();
    }

}

系列八、编程式事务_第2张图片

你可能感兴趣的:(Spring5系列,java,spring)