SpringBoot之事务管理@Transactional

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

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

springboot下的话 搞一个@Transactional即可

转账实例,A用户转账给B用户xx元

Account类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package  com.huawei.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_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余额


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

QQ鎴浘20170808160943.jpg


新建AccountDao接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package  com.huawei.dao;
 
import  org.springframework.data.jpa.repository.JpaRepository;
 
import  com.java1234.entity.Account;
 
/**
  * 账户Dao接口
  * @author user
  *
  */
public  interface  AccountDao  extends  JpaRepository<Account, Integer>{
 
}


AccountService接口

1
2
3
4
5
6
7
8
9
10
11
12
package  com.huawei.service;
 
/**
  * 帐号Service接口
  * @author user
  *
  */
public  interface  AccountService {
 
     public  void  transferAccounts( int  fromUser, int  toUser, float  account);
 
}


AccountServiceImpl接口实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package  com.huawei.service.impl;
 
import  javax.annotation.Resource;
import  javax.transaction.Transactional;
 
import  org.springframework.stereotype.Service;
 
import  com.huawei.dao.AccountDao;
import  com.huawei.entity.Account;
import  com.huawei.service.AccountService;
 
/**
  * 帐号Service实现类
  * @author user
  *
  */
@Service ( "accountService" )
public  class  AccountServiceImpl  implements  AccountService{
 
     @Resource
     private  AccountDao accountDao;

     @Transactional
    //事物处理
     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类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package  com.huawei.controller;
 
import  javax.annotation.Resource;
 
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RestController;
 
import  com.huawei.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 我们先把数据恢复到700  300


猜你喜欢

转载自blog.csdn.net/heguiliang_123/article/details/80148962