Transaction management of SpringBoot @ Transactional

ssh ssm has transaction management service layer configured through applicationContext.xml, and all service methods are added with transaction operations;

It is used to ensure consistency, that is, multiple dao operations in the service method succeed or fail at the same time;

Just make a @ Transactional under springboot

Transfer instance, transfer from user A to user B xx Yuan

Class 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 number userName user name balance balance


Run the startup class. We add two data to the database


New AccountDao interface

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;
 
/**
 * Account Dao interface
 * @author user
 *
 */
public interface AccountDao extends JpaRepository<Account, Integer>{
 
}


AccountService interface

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


AccountServiceImpl interface implementation class

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;
 
/**
 * Account Service implementation class
 * @author user
 *
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService{
 
    @Resource
    private AccountDao accountDao;

    @Transactional
    //Transaction processing
    public void transferAccounts(int fromUserId, int toUserId, float account) {
        Account fromUserAccount=accountDao.getOne(fromUserId);
        fromUserAccount.setBalance(fromUserAccount.getBalance()-account);
        accountDao.save(fromUserAccount); //Deduct money from user
         
        Account toUserAccount=accountDao.getOne(toUserId);
        toUserAccount.setBalance(toUserAccount.getBalance()+account);
        accountDao.save(toUserAccount); //toUser plus
    }
     
     
}


AccountController class

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;
 
/**
 * Account controller
 * @author user
 *
 */
@RestController
@RequestMapping("/account")
public class AccountController {
 
    @Resource
    private AccountService accountService;
     
    @RequestMapping("/transfer")
    public String transferAccounts(){
        try{
            accountService.transferAccounts(12200);
            return "ok";
        }catch(Exception e){
            return "no";
        }
    }
}


We execute the startup class

Browser input: http://localhost:8888/account/transfer

OK, let's restore the data to 700 300


Tags: ssh xml SpringBoot Database

Posted on Sat, 28 Mar 2020 10:51:55 -0400 by bigmichael