goods 商品表
sale 表数据
goods 表数据
项目结构
Step1: maven 依赖 pom.xml
spring3 com.it 1.0-SNAPSHOT 4.0.0 springTransactionspringTransaction http://www.example.com UTF-8 1.8 1.8 junit junit4.11 test org.springframework spring-context5.2.5.RELEASE org.springframework spring-tx5.2.5.RELEASE org.springframework spring-jdbc5.2.5.RELEASE org.mybatis mybatis3.5.1 org.mybatis mybatis-spring1.3.1 mysql mysql-connector-java5.1.9 com.alibaba druid1.1.12 src/main/java ***.xml false src/main/resources ***.xml false maven-compiler-plugin 3.1 1.8 1.8 maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-jar-plugin 3.0.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 maven-site-plugin 3.7.1 maven-project-info-reports-plugin 3.0.0
Step2:创建实体类
实体类Sale
package com.it.entity;
public class Sale {
private Integer id;
private Integer gid;
private Integer nums;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getGid() {
return gid;
}
public void setGid(Integer gid) {
this.gid = gid;
}
public Integer getNums() {
return nums;
}
public void setNums(Integer nums) {
this.nums = nums;
}
@Override
public String toString() {
return "Sale{" +
"id=" + id +
", gid=" + gid +
", nums=" + nums +
'}';
}
}
实体类Goods
package com.it.entity;
public class Goods {
private Integer id;
private String name;
private Integer amount;
private Float price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + ''' +
", amount=" + amount +
", price=" + price +
'}';
}
}
Step3
:定义
dao
接口
(1)GoodsDao
package com.it.dao;
import com.it.entity.Goods;
public interface GoodsDao {
// 更新库存
// goods表示本次用户购买的商品信息,id,购买的数量
int updateGoods(Goods goods);
// 查询商品信息
Goods selectGoods(Integer id);
}
(2)SaleDao
package com.it.dao;
import com.it.entity.Sale;
public interface SaleDao {
// 增加销售记录
int insertSale(Sale sale);
}
Step4
:定义
dao
接口对应的
sql
映射文件
SaleDao.xml
GoodsDao.xmlinsert into sale(gid,nums) values(#{gid},#{nums})
Step5 :定义异常类 定义 service 层可能会抛出的异常类 NotEnoughExceptionupdate goods set amount=amount - #{amount} where id=#{id}
package com.it.exception;
//演示出现运行时异常,发生回滚事件
public class NotEnoughException extends RuntimeException {
public NotEnoughException() {
super();
}
public NotEnoughException(String s) {
super(s);
}
}
Step6
:定义
Service
接口
定义
Service
接口
BuyGoodsService
package com.it.service;
public interface BuyGoodsService {
//购买商品的方法,goodId购买商品的编号,nums:购买商品的数量
void buy(Integer goodsId,Integer nums);
}
Step7
:定义
service
的实现类
定义
service
层接口的实现类
BuyGoodsServiceImpl
package com.it.service.impl;
import com.it.dao.GoodsDao;
import com.it.dao.SaleDao;
import com.it.entity.Goods;
import com.it.entity.Sale;
import com.it.exception.NotEnoughException;
import com.it.service.BuyGoodsService;
public class BuyGoodsServiceImpl implements BuyGoodsService {
private SaleDao saleDao;
private GoodsDao goodsDao;
@Override
public void buy(Integer goodsId, Integer nums) {
System.out.println("=====buy()的开始=======");
// 记录销售信息,向sale表添加记录
Sale sale=new Sale();
sale.setGid(goodsId);
sale.setNums(nums);
saleDao.insertSale(sale);
// 更新库存
Goods goods = goodsDao.selectGoods(goodsId);//更新库存之前,先查找一下库存信息
if (goods==null){
// 商品不存在
throw new NullPointerException("编号是:"+goodsId+"的商品不存在");
}else if (goods.getAmount()
Step8
:修改
Spring
配置文件内容
声明
Mybatis
对象
,声明业务层对象
Step9
:定义测试类
定义测试类
MyTest
。现在就可以在无事务代理的情况下运行了。
package com.it;
import static org.junit.Assert.assertTrue;
import com.it.service.BuyGoodsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppTest
{
// 测试正常购买
@Test
public void test1(){
String config="applicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(config);
// 从容器获取service
BuyGoodsService buyService = (BuyGoodsService) ac.getBean("buyService");
// 调用方法
buyService.buy(1001,10);
}
// 测试购买不存在的商品
@Test
public void test2(){
String config="applicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(config);
BuyGoodsService buyService = (BuyGoodsService) ac.getBean("buyService");
buyService.buy(1003,10);
}
// 测试购买超出库存的商品
@Test
public void test3(){
String config="applicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(config);
BuyGoodsService buyService = (BuyGoodsService) ac.getBean("buyService");
buyService.buy(1002,100);
}
}
测试结果
测试正常购买后
测试购买不存在的商品
测试购买存在的商品,但超出商品库存



