美文网首页
2018-12-13 实现秒杀功能-商品列表页

2018-12-13 实现秒杀功能-商品列表页

作者: 培根好吃 | 来源:发表于2018-12-13 21:14 被阅读0次

    1.数据库设计

    1.1 数据库建表语句

    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=3 DEFAULT charset=utf8mb4;
    
    
    INSERT INTO goods VALUES (1,'iphoneX','Apple iphone X (A1865) 64GB 银色 移动联通电信4G手机','/img/iphonex.png','Apple iphone X (A1865) 64GB 银色 移动联通电信4G手机',8765.00,10000),(2,'华为Meta9','华为Meta9 4GB+32GB版 月光银 移动联通电信4G手机 双卡双待','/img/meta9.png','华为Meta9 4GB+32GB版 月光银 移动联通电信4G手机 双卡双待',3212.00,-1);
    
    
    CREATE TABLE miaosha_goods (
      id bigint(20)  NOT NULL AUTO_INCREMENT COMMENT '秒杀的商品表', 
        goods_id bigint(20) DEFAULT null comment '商品id',
        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=3 DEFAULT charset=utf8mb4;
    
    insert into miaosha_goods values (1,1,0.01,4,'2018-11-05 15:18:00','2018-11-13 14:00:18'),(2,2,0.01,9,'2018-11-12 14:00:14','2018-11-13 14:00:24');
    
    CREATE TABLE order_info (
      id bigint(20)  NOT NULL AUTO_INCREMENT, 
        user_id bigint(20) DEFAULT null comment '用户ID',
        goods_id bigint(20) default null comment '商品ID',
        delivery_addr_id bigint(20) default null comment '收货地址ID',
        goods_name VARCHAR(16) DEFAULT null comment '冗余过来的商品名称',
        goods_count int(11) DEFAULT '0' comment '商品数量',
        goods_price DECIMAL(10,2) DEFAULT '0.00'  COMMENT '商品单价',
        order_channel tinyint(4) DEFAULT '0' comment '1pc,2android,3ios',
        status tinyint(4) DEFAULT '0' comment '订单状态,0新建未支付,1已支付,2已发货,3已收货,4已退款,5已完成',
        create_date datetime DEFAULT null comment '订单的创建时间',
        pay_date datetime DEFAULT null comment '支付时间',
        primary key (id)
    )ENGINE=INNODB auto_increment=12 DEFAULT charset=utf8mb4;
    
    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;
    

    2.domain商品类

    Goods类

    package com.ryan.miaosha.domain;
    
    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 Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getGoodsName() {
            return goodsName;
        }
        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }
        public String getGoodsTitle() {
            return goodsTitle;
        }
        public void setGoodsTitle(String goodsTitle) {
            this.goodsTitle = goodsTitle;
        }
        public String getGoodsImg() {
            return goodsImg;
        }
        public void setGoodsImg(String goodsImg) {
            this.goodsImg = goodsImg;
        }
        public String getGoodsDetail() {
            return goodsDetail;
        }
        public void setGoodsDetail(String goodsDetail) {
            this.goodsDetail = goodsDetail;
        }
        public Double getGoodsPrice() {
            return goodsPrice;
        }
        public void setGoodsPrice(Double goodsPrice) {
            this.goodsPrice = goodsPrice;
        }
        public Integer getGoodsStock() {
            return goodsStock;
        }
        public void setGoodsStock(Integer goodsStock) {
            this.goodsStock = goodsStock;
        }
    }
    
    

    1.MiaoshaGoods类

    1.1

    package com.ryan.miaosha.domain;
    
    import java.util.Date;
    
    public class MiaoshaGoods {
        private Long id;
        private Long goodsId;
        private Integer stockCount;
        private Date startDate;
        private Date endDate;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public Long getGoodsId() {
            return goodsId;
        }
        public void setGoodsId(Long goodsId) {
            this.goodsId = goodsId;
        }
        public Integer getStockCount() {
            return stockCount;
        }
        public void setStockCount(Integer stockCount) {
            this.stockCount = stockCount;
        }
        public Date getStartDate() {
            return startDate;
        }
        public void setStartDate(Date startDate) {
            this.startDate = startDate;
        }
        public Date getEndDate() {
            return endDate;
        }
        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }
    }
    
    

    1.订单详情

    1.1

    package com.ryan.miaosha.domain;
    
    import java.util.Date;
    
    public class OrderInfo {
        private Long id;
        private Long userId;
        private Long goodsId;
        private Long deliveryAddrId;
        private String goodsName;
        private Integer goodsCount;
        private Double goodsPrice;
        private Integer orderChannel;
        private Integer status;
        private Date createDate;
        private Date payDate;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public Long getUserId() {
            return userId;
        }
        public void setUserId(Long userId) {
            this.userId = userId;
        }
        public Long getGoodsId() {
            return goodsId;
        }
        public void setGoodsId(Long goodsId) {
            this.goodsId = goodsId;
        }
        public Long getDeliveryAddrId() {
            return deliveryAddrId;
        }
        public void setDeliveryAddrId(Long deliveryAddrId) {
            this.deliveryAddrId = deliveryAddrId;
        }
        public String getGoodsName() {
            return goodsName;
        }
        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }
        public Integer getGoodsCount() {
            return goodsCount;
        }
        public void setGoodsCount(Integer goodsCount) {
            this.goodsCount = goodsCount;
        }
        public Double getGoodsPrice() {
            return goodsPrice;
        }
        public void setGoodsPrice(Double goodsPrice) {
            this.goodsPrice = goodsPrice;
        }
        public Integer getOrderChannel() {
            return orderChannel;
        }
        public void setOrderChannel(Integer orderChannel) {
            this.orderChannel = orderChannel;
        }
        public Integer getStatus() {
            return status;
        }
        public void setStatus(Integer status) {
            this.status = status;
        }
        public Date getCreateDate() {
            return createDate;
        }
        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }
        public Date getPayDate() {
            return payDate;
        }
        public void setPayDate(Date payDate) {
            this.payDate = payDate;
        }
    
        
    }
    
    

    1.MiaoshaOrder

    1.1

    package com.ryan.miaosha.domain;
    
    public class MiaoshaOrder {
       private Long id;
       private Long userId;
       private Long orderId;
       private Long goodsId;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    public Long getOrderId() {
        return orderId;
    }
    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }
    public Long getGoodsId() {
        return goodsId;
    }
    public void setGoodsId(Long goodsId) {
        this.goodsId = goodsId;
    }
    }
    
    

    1.GoodsVo 它包含了Goods和秒杀商品 联合查询

    1.1

    package com.ryan.miaosha.vo;
    
    import java.util.Date;
    
    import com.ryan.miaosha.domain.Goods;
    
    public class GoodsVo extends Goods {
        private Integer stockCount;
        private Date startDate;
        private Date endDate;
        public Integer getStockCount() {
            return stockCount;
        }
        public void setStockCount(Integer stockCount) {
            this.stockCount = stockCount;
        }
        public Date getStartDate() {
            return startDate;
        }
        public void setStartDate(Date startDate) {
            this.startDate = startDate;
        }
        public Date getEndDate() {
            return endDate;
        }
        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }
    }
    
    

    1.goods.html

    1.1

    <!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>
    
    <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="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>
    
    

    1.dao层

    1.1

    package com.ryan.miaosha.dao;
    
    
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    import com.ryan.miaosha.vo.GoodsVo;
    
    @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> listGoodsVo();
    }
    
    

    1.service层

    1.1

    package com.ryan.miaosha.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.ryan.miaosha.dao.GoodsDao;
    import com.ryan.miaosha.dao.User;
    import com.ryan.miaosha.dao.UserDao;
    import com.ryan.miaosha.vo.GoodsVo;
    
    @Service
    public class GoodsService {
        @Autowired
        GoodsDao goodsDao;
        public List<GoodsVo> listGoodsVo() {
            return goodsDao.listGoodsVo();
        }
    }
    
    

    1.controller层

    1.1

    package com.ryan.miaosha.controller;
    
    import java.util.List;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.CookieValue;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import com.alibaba.druid.util.StringUtils;
    import com.ryan.miaosha.dao.MiaoshaUser;
    import com.ryan.miaosha.redis.RedisService;
    import com.ryan.miaosha.service.GoodsService;
    import com.ryan.miaosha.service.MiaoshaService;
    import com.ryan.miaosha.vo.GoodsVo;
    
    @Controller
    @RequestMapping("/goods")
    public class GoodsController {
        @Autowired
        RedisService redisService;
        @Autowired
        GoodsService goodsService;
        
        @RequestMapping("/to_list")
        String list(Model model,MiaoshaUser user) {     
            model.addAttribute("user", user);
            
            List<GoodsVo> goodsList=goodsService.listGoodsVo();
            model.addAttribute("goodsList", goodsList);
            return "goods_list";
        }
    }
    
    

    相关文章

      网友评论

          本文标题:2018-12-13 实现秒杀功能-商品列表页

          本文链接:https://www.haomeiwen.com/subject/fbxihqtx.html