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
但是数据库数据没变化 说明启动作用了。
耐得住寂寞,才能登得顶
Gitee码云:https://gitee.com/lyc96/projects