1-6SpringBoot之事务管理@Transactional

以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作;

用来保证一致性,即service方法里的多个dao操作,要么同时成功,要么同时失败;

 

springboot下的话 搞一个@Transactional即可;

 

我们这里搞一个实例,转账实例,A用户转账给B用户xx元

 

设计如下:

Account类

 

import javax.persistence.Column;

import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.Id;
import  javax.persistence.Table;
 
@Entity
@Table (name= "t_account" )
public  class  Account {
 
     @Id
     @GeneratedValue
     private  Integer id;
     
     @Column (length= 50 )
     private  String userName;
     
     private  float  balance;
 
     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  float  getBalance() {
         return  balance;
     }
 
     public  void  setBalance( float  balance) {
         this .balance = balance;
     }
 
     
     
     
}
 

id 编号 userName用户名 balance余额

 

运行启动类,数据库里我们加两个数据

 

 

新建AccountDao接口

 

import org.springframework.data.jpa.repository.JpaRepository;

 
import  com.java1234.entity.Account;
 
/**
  * 账户Dao接口
  * @author user
  *
  */
public  interface  AccountDao  extends  JpaRepository<Account, Integer>{
 
}
 
AccountService接口
 
/**
  * 帐号Service接口
  * @author user
  *
  */
public  interface  AccountService {
 
     public  void  transferAccounts( int  fromUser, int  toUser, float  account);
 
}
 
AccountServiceImpl接口实现类
 
import  javax.annotation.Resource;
import  javax.transaction.Transactional;
 
import  org.springframework.stereotype.Service;
 
import  com.java1234.dao.AccountDao;
import  com.java1234.entity.Account;
import  com.java1234.service.AccountService;
 
/**
  * 帐号Service实现类
  * @author user
  *
  */
@Service ( "accountService" )
public  class  AccountServiceImpl  implements  AccountService{
 
     @Resource
     private  AccountDao accountDao;
 
     public  void  transferAccounts( int  fromUserId,  int  toUserId,  float  account) {
         Account fromUserAccount=accountDao.getOne(fromUserId);
         fromUserAccount.setBalance(fromUserAccount.getBalance()-account);
         accountDao.save(fromUserAccount);  // fromUser扣钱
         
         Account toUserAccount=accountDao.getOne(toUserId);
         toUserAccount.setBalance(toUserAccount.getBalance()+account);
         accountDao.save(toUserAccount);  // toUser加钱
     }
     
     
}
 
AccountController类
 
import  javax.annotation.Resource;
 
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RestController;
 
import  com.java1234.service.AccountService;
 
/**
  * 账户Controoler类
  * @author user
  *
  */
@RestController
@RequestMapping ( "/account" )
public  class  AccountController {
 
     @Resource
     private  AccountService accountService;
     
     @RequestMapping ( "/transfer" )
     public  String transferAccounts(){
         try {
             accountService.transferAccounts( 1 2 200 );
             return  "ok" ;
         } catch (Exception e){
             return  "no" ;
         }
     }
}
 

我们执行启动类

浏览器输入:http://localhost:8888/account/transfer

运行OK

 

 

OK 我们先把数据恢复到700  300

现在我们把service层方法改下 

 

 

这时候 扣钱dao能执行成功  加钱操作执行不了了 因为前面会报错。

 

我们重启启动类

浏览器输入:http://localhost:8888/account/transfer

运行NO

 

这时候 钱扣了 但是 没加钱  导致了数据不一致性

 

这时候 我们需要用上事务

在service方法上加上@Transactional即可

 

 

我们恢复下数据700  300

然后再重启启动类,

浏览器输入:http://localhost:8888/account/transfer

运行NO

 

但是数据库数据没变化 说明启动作用了。

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/chenlove/p/8708499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@TransactionalSpring框架中用于配置事务的注解之一,它可以被应用于类或方法上。当被应用于类上时,该类中的所有公共方法都将被配置为支持事务。当被应用于方法上时,该方法将被配置为支持事务。@Transactional注解提供了多个属性,可以用于配置事务的各个方面,例如隔离级别、传播行为、只读状态等。以下是一个使用@Transactional注解配置事务的例子: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false) public void addUser(User user) { userDao.addUser(user); } @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false) public void updateUser(User user) { userDao.updateUser(user); } @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false) public void deleteUser(int userId) { userDao.deleteUser(userId); } @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT, readOnly = true) public User getUserById(int userId) { return userDao.getUserById(userId); } @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT, readOnly = true) public List<User> getAllUsers() { return userDao.getAllUsers(); } } ``` 在上面的例子中,@Transactional注解被应用于UserServiceImpl类中的所有公共方法上。其中,propagation属性指定了事务的传播行为,isolation属性指定了事务的隔离级别,readOnly属性指定了事务是否只读。在这个例子中,事务的传播行为被配置为REQUIRED,表示如果当前没有事务,则创建一个新的事务;如果当前已经存在事务,则加入该事务事务的隔离级别被配置为DEFAULT,表示使用数据库默认的隔离级别。事务的只读状态被配置为false,表示该事务可以读取和修改数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值