第一部分 数据库
数据库的表格为:
goods、 miaosha_goods、miaosha_order、 miaosha_user
goods 表格代码:
DROP TABLE IF EXISTS `goods`;
CREATE TABLE `goods` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品ID',
`goods_name` varchar(16) DEFAULT NULL COMMENT '商品名称',
`goods_title` varchar(64) DEFAULT NULL COMMENT '商品标题',
`goods_img` varchar(64) DEFAULT NULL COMMENT '商品的图片',
`goods_detail` longtext COMMENT '商品的详情介绍',
`goods_price` decimal(10,2) DEFAULT '0.00' COMMENT '商品单价',
`goods_stock` int(11) DEFAULT '0' COMMENT '商品库存, -1表示没有限制',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of goods
-- ----------------------------
INSERT INTO `goods` VALUES ('3', 'iphone11', '苹果11', '/img/11.png', '苹果', '10000.00', '10');
INSERT INTO `goods` VALUES ('4', 'p40', '华为p40', '/img/p40.png', '华为', '6688.00', '10');
这个是miaosha_goods的数据库
-- ----------------------------
-- Table structure for miaosha_goods
-- ----------------------------
DROP TABLE IF EXISTS `miaosha_goods`;
CREATE TABLE `miaosha_goods` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '秒杀的商品表',
`goods_id` bigint(20) DEFAULT NULL COMMENT '商品ld',
`miaosha_price` decimal(10,2) DEFAULT '0.00' COMMENT '秒杀价',
`stock_count` int(11) DEFAULT NULL COMMENT '库存数量',
`start_date` datetime DEFAULT NULL COMMENT '秒杀开始时间',
`end_date` datetime DEFAULT NULL COMMENT '秒杀结束时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of miaosha_goods
-- ----------------------------
INSERT INTO `miaosha_goods` VALUES ('3', '3', '1.00', '1', '2020-05-29 07:28:59', '2020-07-05 12:12:27');
INSERT INTO `miaosha_goods` VALUES ('4', '4', '2.00', '1', '2020-05-29 16:28:13', '2020-06-06 12:12:18');
这部分是miaosha_order的数据表
DROP TABLE IF EXISTS `miaosha_order`;
CREATE TABLE `miaosha_order` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL COMMENT '用户ID',
`order_id` bigint(20) DEFAULT NULL COMMENT '订单ID',
`goods_id` bigint(20) DEFAULT NULL COMMENT '商品ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
miaosha_user表格创建
CREATE TABLE `miaosha_user` (
`id` bigint(20) NOT NULL COMMENT '用户id,手机号码',
`nickname` varchar(255) NOT NULL,
`password` varchar(32) DEFAULT NULL COMMENT 'MD5,pass明文 固定salt',
`salt` varchar(10) DEFAULT NULL,
`head` varchar(128) DEFAULT NULL COMMENT '头像,云存储的id',
`register_date` datetime DEFAULT NULL COMMENT '注释的时间',
`last_login_date` datetime DEFAULT NULL COMMENT '上次登录时间',
`login_count` int(11) DEFAULT '0' COMMENT '登录次数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of miaosha_user
-- ----------------------------
INSERT INTO `miaosha_user` VALUES ('18812345678', 'laosan', 'b7797cce01b4b131b433b6acf4add449', '1a2b3c4d', null, '2020-05-12 15:16:39', '2020-05-12 15:16:45', '1');
第二部分: Domain层类文件的编写
按照数据库的设计,在domain中编写相关的类//可以使用工具自动生成
对应于数据库的设计
分别为:Goods类、MiaoshaGoods、MiaoshaUser、 MiaoshaInfo 、 MiaoshaOrder
各个类的代码:
public class Goods {
private Long id;
private String goodsName;
private String goodsTitle;
private String goodsImg;
private String goodsDetail;
private Double goodsPrice;
private Integer goodsStock;
public class MiaoshaGoods {
private Long id;
private Long goodsld;
private Integer stockCount;
private Date startDate;
public class OrderInfo {
private Long id;
private Long userld;
private Long goodsld;
private Long deliveryAddrld;
private String goodsName;
private Integer goodsCount;
private Double goodsPrice;
private Integer orderChannel;
private Integer status;
private Date createDate;
private Date payDate;
public class MiaoshaOrder {
private Long id;
private Long userld;
private Long orderld;
private Long goodsld;
public class MiaoshaUser {
private Long id;
private String nickname;
private String password;
private String salt;
private String head;
private Date registerDate;
private Date lastLoginDate;
private Integer loginCount;
private String deliveryAddress;
第三部分: VO的编写
将基本的Class生成之后,考虑到秒杀系统的秒杀页面不是一个数据库的信息,因此创建一个新的class,GoodsVo 封装秒杀所需要的信息
public class GoodsVo extends Goods {
private Integer stockCount;
private Date startDate;
private Date endDate;
private Double miaoshaPrice;
private String img;
第四部分: Dao层
考虑到商品的服务类:GoodService服务类需要注入GoodDao,
GoodDao层:
@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> listGoodsBoList();
@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} "})
GoodsVo getGoodsVoByGoodsId(@Param("goodsId") long goodsId);
}
第五部分 ;Service层
GoodsService:
@Service
public class GoodsService {
@Autowired
GoodsDao goodsDao;
public List<GoodsVo> listGoodVo(){
return goodsDao.listGoodsBoList();
}
public GoodsVo getGoodsVoByGoodsId(long goodsId) {
return goodsDao.getGoodsVoByGoodsId(goodsId);
}
第六部分: HTML 前端页面的书写
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>商品列表</title>
<meta http-equiv="Content-Type " content="text/html;charset=UTF-8"/>
<!-- jquery -->
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<!-- bootstrap -->
<link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.min.css}" />
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
<!-- jquery-validator -->
<script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}"></script>
<script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}"></script>
<!-- layer -->
<script type="text/javascript" th:src="@{/layer/layer.js}"></script>
<!-- md5.js -->
<script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
<!-- common.js -->
<script type="text/javascript" th:src="@{/js/common.js}"></script>
</head>
<body>
<!--/*@thymesVar id="user" type="com.test.miaosha.dao.UserDao"*/-->
<p th:text="'hello:'+${user.nickname}"></p>
<div class = "panel panel-delault">
<div class="panel-heading" >秒杀商品列表</div>
<table class= "table" id="goodslist">
<tr>
<td>商品名称</td>
<td>商品图片</td>
<td>商品原价</td>
<td>秒杀价</td>
<td>库存数量</td>
<td>详情</td>
</tr>
<!--/*@thymesVar id="goodsList" type="com.test.miaosha"*/-->
<tr th:each="goods,goodsStat:${goodsList}">
<td th:text="${goods.goodsName}"></td>
<td ><img th:src="@{${goods.goodsImg}}" width="100" height="100"/></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>
///
Demo展示
不要忘了设置图片的路径!!!!
最终的展示页面:
版权声明:本文为weixin_40170393原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。