zoukankan      html  css  js  c++  java
  • SpringBoot学习笔记之事务管理

    新建SpringBoot项目SpringDataTransaction

    新建个数据库db_bank  模拟银行转账,张三账户给李四转账,那么张三账户金额要减少,李四账户金额要增加,且张三减少的金额等于李四增加的金额

    1.新建个Account.java账户实体 

    package com.guo.entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity     // 实体
    @Table(name="t_count")   //
    public class Account {
        @Id      // 组件
        @GeneratedValue   //表 自增
        private Integer id;    // 主键
        @Column(length=50)   // 对应字符串   长度100(自定义)
        private String userName;  //用户名
        private Integer count;  // 存款金额
    
        
        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 Integer getCount() {
            return count;
        }
    
        public void setCount(Integer count) {
            this.count = count;
        }    
    
    }

     

    2.新建AccountDao 接口

    package com.guo.dao;
    
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    
    import com.guo.entity.Account;
    
                                    
    public interface AccountDao extends JpaRepository<Account,Integer>{
      
    }

    3.这把多了个service接口

    package com.guo.service;
    
    public interface AccountService {
        
        //声明个客户转账的方法
        public void tranMoney(int fromUser,int toUser,int count);
    
    }

     

    4.service接口实现类

    package com.guo.service.impl;
    
    import javax.annotation.Resource;
    import javax.transaction.Transactional;
    
    import org.springframework.stereotype.Service;
    
    import com.guo.dao.AccountDao;
    import com.guo.entity.Account;
    import com.guo.service.AccountService;
    @Service("AccountService")
    public class AccountServiceImpl implements AccountService{
        @Resource
        private AccountDao accountDao;
        
        @Transactional
        public void tranMoney(int fromUser, int toUser, int count) {
            Account fromAccount=accountDao.getOne(fromUser);
            fromAccount.setCount(fromAccount.getCount()-count);
            accountDao.save(fromAccount);
            Account toAccount=accountDao.getOne(toUser);
            toAccount.setCount(toAccount.getCount()+count);
            int zero=1/0;  // 加的异常
            accountDao.save(toAccount);
            
        }
    
    }

     

    5.新建个AccountController

    package com.guo.service.impl;
     
    import javax.annotation.Resource;
    import javax.transaction.Transactional;
     
    import org.springframework.stereotype.Service;
     
    import com.guo.dao.AccountDao;
    import com.guo.entity.Account;
    import com.guo.service.AccountService;
    @Service("AccountService")
    public class AccountServiceImpl implements AccountService{
        @Resource
             private AccountDao accountDao;
             
        @Transactional
             public void tranMoney(int fromUser, int toUser, int count) {
                 Account fromAccount=accountDao.getOne(fromUser);
                       fromAccount.setCount(fromAccount.getCount()-count);
                       accountDao.save(fromAccount);
                       Account toAccount=accountDao.getOne(toUser);
                       toAccount.setCount(toAccount.getCount()+count);
                       //搞个异常int zero=1/0;
                       accountDao.save(toAccount);
                       
             }
     
    }

    6.测试

    刚开始张三和李四 账户各有500

    如果加个异常int zero=1/0;

    Service.impl不加 @Transactional标签时 会报异常

    运行结果为NO,此时张三账户少了200,但是李四账户并没多200

    加个@Transactional,运行虽结果虽为NO 但是张三少200,同时李四多200,体现了事务的一致性

    本博客为博主的学习笔记,不作任何商业用途。
  • 相关阅读:
    Oracle存储过程(Stored Procedure)使用自定义类实体参数
    RTF格式富文本图片文本导出到Excel
    将EXCEL表格文字图片信息转成RTF格式写入数据库BLOB字段
    C# 阿拉伯数字转换为中文数字/中文数字转换为阿拉伯数字
    JavaScript操作select下拉框
    JavaScript操作浏览器一直往下滚动
    Microsoft Office 2003 Web Components 自动计数/自动求和
    SQLite递归
    WPF获取验证码倒计时
    System.UnauthorizedAccessException: 对路径“***”的访问被拒绝。
  • 原文地址:https://www.cnblogs.com/guo7533/p/8727212.html
Copyright © 2011-2022 走看看