秒杀商城系统 商品列表页的设计 (十)

数据库商品为什么要分商品表和秒杀商品表?

假设商品和秒杀商品都在一张表中,秒杀活动经常以不同的形式展开,数据库字段也要经常改啊,那和数据库有关的老代码是不是也要一起改,当数据库不断修改,后期维护也比较困难。

前端goods_list.html页面代码:

<!DOCTYPE html>
<!-- 使用thymeleaf,配置相应的 -->
<html xmlns:th="http://www.thymeleaf.org">  
<head>
<meta charset="UTF-8"/>
<title>商品详情</title>
</head>
<body>
	<div class="panel panel-default">
		<div class="panel-heading">秒杀商品列表</div>
		<table class="table" id="goodslist">
		<tr><td>商品名称</td><td>商品图片</td><td>商品原价</td><td>秒杀价</td><td>库存数量</td><td>详情</td></tr>
		<tr th:each="goods,goodsStat:${goodsList}">
			<td th:text="${goods.goodsName}"></td>
			<td><img th:src="@{${goods.goodsImg}}" width="80" height="60"></img></td>
			<td th:text="${goods.goodsPrice}"></td>
			<td th:text="${goods.miaoshaPrice}"></td>
			<td th:text="${goods.stockCount}"></td>
			<td><a th:href="'/goods/to_detail/'+${goods.id}">详情</a></td>
		</tr>
		</table>
	</div>
</body>
</html>

先创建商品的服务类GoodsService ,并注入GoodsDao

	@Service
	public class GoodsService {
		public static final String COOKIE1_NAME_TOKEN="token";	
		@Autowired
		GoodsDao goodsDao;
		@Autowired
		RedisService redisService;
		//获取商品信息列表		
		public List<GoodsVo>  getGoodsVoList() {
			return goodsDao.getGoodsVoList();
		}	
		//获取商品根据商品Id
		public GoodsVo getGoodsVoByGoodsId(long goodsId) {
			return goodsDao.getGoodsVoByGoodsId(goodsId);
		}
		//减少商品的库存
		public void reduceStock(GoodsVo goodsvo) {
			MiaoshaGoods goods=new MiaoshaGoods();
			goods.setGoodsId(goodsvo.getId());
			//goods.setStockCount(goodsvo.getGoodsStock()-1);  sql里面去运算
			//goodsDao.reduceStock(goods.getGoodsId());
			goodsDao.reduceStock(goods);
		}	
	}

GoodsVo对象

注意:这里我们查数据库的时候,不只是查找的商品的信息,我们同时想把商品的秒杀信息也一起查出来,但是这两个不同数据在两个表里面,我们就想办法封装一个GoodsVo,将两张表的数据封装到一起。

继承Goods,拥有Goods的所有字段,然后再自己定义MiaoshaGoods里面的字段,最终拼接成一个GoodsVo对象。

	//将Goods表和MiaoshaGoods表合并
	@Getter
	@Setter
	public class GoodsVo extends Goods{
	private Integer stockCount;
	private Date startDate;
	private Date endDate;
	private Double miaoshaPrice;   //实际业务中秒杀价格是整数,这里是为了方便演示
	}

创建GoodsDao

  • 这里是将两个表做连接查询(miaosha_goods mg left join goods g),我们需要查找的数据库表里面的各个字段对应到我们GoodsVo里面的属性,那么就可以给其赋值。
  • g.*,mg.stock_count,mg.start_date,mg.end_date,mg.miaosha_price:代表goods 表里面的所有字段加上miaosha_goods里面的stock_count,start_date,end_date,miaosha_price
	@Mapper
	public interface GoodsDao {

	@Select("select g.*,mg.stock_count,mg.start_date,mg.end_date,mg.miaosha_price from miaosha_goods mg left join goods g on mg.goods_id=g.id")  
	public List<GoodsVo> getGoodsVoList();
	
	@Select("select g.*,mg.stock_count,mg.start_date,mg.end_date,mg.miaosha_price from miaosha_goods mg left join goods g on mg.goods_id=g.id where g.id=#{goodsId}")  
	public GoodsVo getGoodsVoByGoodsId(@Param("goodsId")long goodsId);//使用注解@Param(“goodsId”)指定@Select语句中的#{goodsId}字段,一一对应起来
	
	//stock_count>0的时候才去更新,数据库本身会有锁,那么就不会在数据库中同时多个线程更新一条记录,使用数据库特性来保证超卖的问题
	@Update("update miaosha_goods set stock_count=stock_count-1 where goods_id=#{goodsId} and stock_count>0")
	public void reduceStock(MiaoshaGoods goods);    
	}
posted @   长勺  阅读(133)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示