17_Spring_事务环境搭建

17_Spring_事务环境搭建

通过张三给李四转账案例演示事务的控制

1 数据库中准备表格

image

applicationContext.xml

jdbc.properties

见上节课

2 项目中准备实体类

  1. package com.msb.pojo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.io.Serializable;
  6. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  7. */
  8. @AllArgsConstructor
  9. @NoArgsConstructor
  10. @Data
  11. public class Account implements Serializable {
  12. private Integer id;
    
  13. private String name;
    
  14. private Integer money;
    
  15. }

3 准备DAO层,创建一个根据id修改money的方法

  1. package com.msb.dao;

  2. /**

    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  3. */

  4. public interface AccountDao {

  5.  int transMoney(int id,int money);
    
  6. }

  7. package com.msb.dao.impl;

  8. import com.msb.dao.AccountDao;

  9. import org.springframework.beans.factory.annotation.Autowired;

  10. import org.springframework.jdbc.core.JdbcTemplate;

  11. import org.springframework.stereotype.Repository;

  12. /**

    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  13. */

  14. @Repository

  15. public class AccountDaoImpl implements AccountDao {

  16. @Autowired
    
  17. private JdbcTemplate jdbcTemplate;
    
  18. @Override
    
  19. public int transMoney(int id, int money) {
    
  20.     String sql ="update account set money =money +? where id =?";
    
  21.     return jdbcTemplate.update(sql,money,id);
    
  22. }
    
  23. }

4 准备Service,创建一个转账的业务方法

  1. package com.msb.service;

  2. /**

    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  3. */

  4. public interface AccountService {

  5.  int transMoney(int from ,int to,int money);
    
  6. }

  7. package com.msb.service.impl;

  8. import com.msb.dao.AccountDao;

  9. import com.msb.service.AccountService;

  10. import org.springframework.beans.factory.annotation.Autowired;

  11. import org.springframework.stereotype.Service;

  12. /**

    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  13. */

  14. @Service

  15. public class AccountServiceImpl implements AccountService {

  16. @Autowired
    
  17. private AccountDao accountDao;
    
  18. @Override
    
  19. public int transMoney(int from, int to, int money) {
    
  20.     int rows=0;
    
  21.     rows+=accountDao.transMoney(from, 0 - money);       
    
  22.     rows+=accountDao.transMoney(to, money);        
    
  23.     return rows;
    
  24. }
    
  25. }

5 测试代码,测试转账

  1. package com.msb.test;
  2. import com.msb.service.AccountService;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  7. */
  8. public class TestTx {
  9. @Test()
    
  10. public void testTransaction(){
    
  11.     ApplicationContext context =new
    
    ClassPathXmlApplicationContext("applicationContext.xml");
  12.     AccountService accountService =
    
    context.getBean(AccountService.class);
  13.     int rows = accountService.transMoney(1, 2, 100);
    
  14.     System.out.println(rows);
    
  15. }
    
  16. }

posted @   AidenDong  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示