详述Spring 框架事务 @Transactional常用属性说明

目录

 

一、引入

BookDao:

 MoneyDao:

CouponDao: 

 CouponService:

二、Transactional注解的各项属性

(1)timeout属性

book表:

money表: 

coupon表:

(2)readOnly属性

(3)rollbackFor属性

(4)propagation属性


一、引入

引入一个场景:

当我们在网上购买图书时,需要两个条件成立才能完成付款,创建订单,即余额足够,且书本余量足够时才能完成付款,创建订单。如下列程序:

首先,创建三个表:book,money,coupon

create table book(
	id char(36) primary key comment '主键',
	name varchar(12) comment '书名',
	quantity int(5) comment '数量',
	price  float(5,2) comment '单价'
);
create table money(
	id char(36) primary key comment '主键',
	user_id char(36) comment '外键,用户id',
	balance float(5,2) comment '余额'
);
create table coupon(
	id char(36) primary key comment '主键',
	user_id char(36) comment '外键,用户id',
	book_id char(36) comment '外键,书籍id',
	total  float(5,2) comment '总额'
);

编写java代码:

BookDao:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.jd.exception.BookException;

@Component
public class BookDao implements IBookDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean enough(String id, int count)  {
		String sql="select quantity from book where id=?";
		int quantity = jdbcTemplate.queryForObject(sql, Integer.class,id);
		if(quantity<count) {//库存不足
			throw new BookException("库存不足,购买失败......");
		}
		return true;
	}
	
	@Override
	public double getPrice(String id) {
		String sql="select price from book where id=?";
		return jdbcTemplate.queryForObject(sql, Double.class,id);
	}
	
	@Override
	public boolean update(String id, int count) {
		String sql = "update book set quantity = quantity- "+count+" where id=?";
		return jdbcTemplate.update(sql, id)>0;
	}

}

 MoneyDao:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.jd.exception.MoneyException;

@Component
public class MoneyDao implements IMoneyDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean enough(String id, double total) {
		String sql="select balance from money where user_id=?";
		Double balance = jdbcTemplate.queryForObject(sql, Double.class,id);
		if(balance<total) {//余额不足
			throw new MoneyException("余额不足,购买失败......");
		}
		return true;
	}
	
	@Override
	public boolean update(String userId, double total) {
		String sql = "update money set balance = balance- "+total+" where user_id=?";
		return jdbcTemplate.update(sql, userId)>0;
	}
}

CouponDao: 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.jd.vo.Coupon;

@Component
public class CouponDao implements ICouponDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(Coupon coupon) {
		String sql = "insert into coupon (id,user_id,book_id,total) values (?,?,?,?)";
		return jdbcTemplate.update(sql, coupon.getId(),coupon.getUserId(),coupon.getBookId(),coupon.getTotal())>0;
	}
}

 CouponService:

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jd.book.dao.IBookDao;
import com.jd.coupon.dao.ICouponDao;
import com.jd.money.dao.IMoneyDao;
import com.jd.vo.Coupon;

@Service
public class CouponService implements ICouponService {

	@Autowired
	private IBookDao bookDao;
	@Autowired
	private IMoneyDao moneyDao;
	@Autowired
	private ICouponDao couponDao;
	
	//立即购买
	@Override
	@Transactional//添加该注解不仅为其创建代理对象,而且在该方法中引入事务
	public boolean insert(String userId,String bookId, int count){
		
		if(bookDao.enough(bookId, count)) {//书籍足够
			//书籍表库存递减
			bookDao.update(bookId, count);
		}
		dou
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值