第六节:SpringBoot之事务管理@Transactional
2017-09-04 17:39
429 查看
代码如下
package com.xiaowen.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="tb_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;
}
}
dao
package com.xiaowen.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.xiaowen.model.Account;
/**
* 账户Dao
* @author xiaowen
*/
public interface AccountDao extends JpaRepository<Account, Integer> {
}
service
package com.xiaowen.service;
/**
* 账户service接口
* @author xiaowen
*/
public interface AccountService {
public void transferAccounts(int fromUser,int toUser,float account);
}
service实现类
package com.xiaowen.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xiaowen.dao.AccountDao;
import com.xiaowen.model.Account;
import com.xiaowen.service.AccountService;
/**
* 帐号Service实现类
* @author xiaowen
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Resource
private AccountDao accountDao;
@Transactional//事务
public void transferAccounts(int fromUser, int toUser, float account) {
Account fromUserAccount=accountDao.getOne(fromUser);
fromUserAccount.setBalance(account);
accountDao.save(fromUserAccount);//扣钱
Account toUserAccount=accountDao.getOne(toUser);
toUserAccount.setBalance(toUserAccount.getBalance()+account);
int k=1/0;
accountDao.save(toUserAccount);//加钱
}
}
controller
package com.xiaowen.controller;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xiaowen.service.AccountService;
@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";
}
}
}
package com.xiaowen.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="tb_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;
}
}
dao
package com.xiaowen.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.xiaowen.model.Account;
/**
* 账户Dao
* @author xiaowen
*/
public interface AccountDao extends JpaRepository<Account, Integer> {
}
service
package com.xiaowen.service;
/**
* 账户service接口
* @author xiaowen
*/
public interface AccountService {
public void transferAccounts(int fromUser,int toUser,float account);
}
service实现类
package com.xiaowen.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xiaowen.dao.AccountDao;
import com.xiaowen.model.Account;
import com.xiaowen.service.AccountService;
/**
* 帐号Service实现类
* @author xiaowen
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Resource
private AccountDao accountDao;
@Transactional//事务
public void transferAccounts(int fromUser, int toUser, float account) {
Account fromUserAccount=accountDao.getOne(fromUser);
fromUserAccount.setBalance(account);
accountDao.save(fromUserAccount);//扣钱
Account toUserAccount=accountDao.getOne(toUser);
toUserAccount.setBalance(toUserAccount.getBalance()+account);
int k=1/0;
accountDao.save(toUserAccount);//加钱
}
}
controller
package com.xiaowen.controller;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xiaowen.service.AccountService;
@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";
}
}
}
相关文章推荐
- Spring Boot中的事务管理
- Spring事务管理中@Transactional的propagation参数
- Spring Boot 中使用 @Transactional 注解配置事务管理
- Spring Boot中的事务管理
- Spring事务管理中@Transactional
- 09 Spring 事务管理(@Transactional)基本使用
- spring配置事务管理及事物传播行为介绍(@Transactional(propagation = Propagation.SUPPORTS))
- Spring Boot中的事务管理
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十七)Spring Boot中的事务管理
- SpringBoot第5小节:事务管理
- 09 Spring 事务管理(@Transactional)基本使用
- Spring @Transactional 声明式事务管理 getCurrentSession
- springboot+mybatis 事务管理
- JavaEE进阶知识学习-----SpringBoot基础知识-5-事务管理知识
- Spring @Transactional 声明式事务管理 getCurrentSession
- 09 Spring 事务管理(@Transactional)基本使用
- java 调用Oracle 存储过程(SpringBoot下的事务管理)
- spring boot配置mybatis和事务管理
- Spring boot jpa 删除数据和事务管理的问题实例详解
- Spring事务管理中@Transactional的参数配置