目录
一、引入
引入一个场景:
当我们在网上购买图书时,需要两个条件成立才能完成付款,创建订单,即余额足够,且书本余量足够时才能完成付款,创建订单。如下列程序:
首先,创建三个表: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