java1234开源博客系统
博客信息

SpringBoot之事务管理@Transactional

0
发布时间:『 2017-08-08 16:34』  博客类别:SpringBoot  阅读(9946) 评论(0)

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

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


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


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


设计如下:

Account类

package com.java1234.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接口

package com.java1234.dao;

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

import com.java1234.entity.Account;

/**
 * 账户Dao接口
 * @author user
 *
 */
public interface AccountDao extends JpaRepository<Account, Integer>{

}


AccountService接口

package com.java1234.service;

/**
 * 帐号Service接口
 * @author user
 *
 */
public interface AccountService {

	public void transferAccounts(int fromUser,int toUser,float account);

}


AccountServiceImpl接口实现类

package com.java1234.service.impl;

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类

package com.java1234;

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


QQ鎴浘20170808162124.jpg


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

现在我们把service层方法改下 

QQ鎴浘20170808162329.jpg


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


我们重启启动类

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

运行NO


QQ鎴浘20170808162520.jpg


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


这时候 我们需要用上事务

在service方法上加上@Transactional即可


QQ鎴浘20170808162625.jpg


我们恢复下数据700  300

然后再重启启动类,

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

运行NO


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



关键字:   SpringBoot     事务管理     @Transactional  
关注Java1234微信公众号
博主信息
Java1234_小锋
(知识改变命运,技术改变世界)
Powered by Java1234 V3.0 Copyright © 2012-2016 Java知识分享网 版权所有