3-1 数据库设计

1、商品表

create table `goods`(

`id` bigint(20) primary key 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表示没有限制'

) ENGINE = InnoDB auto_increment=3 default charset=utf8mb4;

在插入两条记录:

insert into `goods` values(1, 'iphoneX', 'Apple iPhone X(A1865)64GB 银色 移动联通电信4G手机', '/img/iphoneX.png', 'Apple iPhoneX(A1865) 64GB银色 移动联通电信4G手机',8765, 100);
insert into `goods` values(2, '华为Mate9', '华为Mate9 4GB + 32GB', '/img/meta9.png', '华为Mate9 4GB + 32GB',3212, 10);

2、秒杀商品表

create table `miaosha_goods`(
`id` bigint(20) primary key 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 '秒杀结束时间'
) engine = InnoDB auto_increment=3 default charset=utf8mb4;

insert into `miaosha_goods` values(1,1,0.01,4,'2017-11-05 15:18:00', '2017-11-13 14:00:18'),(2,2,0.01,9,'2017-11-12 14:00','2017-11-13 14:00:24');

3、订单表

create table `order_info`(
`id` bigint(20) primary key 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 '支付时间'

)engine = InnoDB auto_increment=12 default charset=utf8mb4;

4、秒杀商品订单表

create table `miaosha_order`(
`id` bigint(20) primary key 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'

)engine = InnoDB auto_increment=12 default charset=utf8mb4;

5、从数据库中使用左连接进行查询,需要一个实体类与之对应。GoodsVo

package com.mydre.miaosha.vo;
import java.util.Date;
import com.mydre.miaosha.domain.Goods;
public class GoodsVo  extends Goods{
private Double miaoshaPrice;
private Integer stockCount;
private Date startDate;
private Date endDate;

}

6、GoodsDao定义如下;

@Mapper
public interface GoodsDao {
/*
* 使用miaosha_goods 左连接 goods,则我们显式select的miaosha_goods中的所有的元组都保留了下来,如果goods中没有对应的,则使用null代替
* miaosha_goods中的goods_id对应着goods中的id,因为大前提是goods,有了goods才能进行秒杀。所以,goods相当于大树的树根,miaosha_goods相当于大树的叶子。可以没有树根,但是一定不能缺少树根。根据叶子进行连接,根据树根来获取连接得到的信息。

* */

@Select("select g.*, mg.stock_count, mg.miaosha_price, mg.start_date, mg.end_date from miaosha_goods mg left join goods g on mg.goods_id = g.id")
public List<GoodsVo> listGoodsVo();
@Select("select g.*, mg.stock_count, mg.miaosha_price, mg.start_date, mg.end_date from miaosha_goods mg left join goods g on mg.goods_id = g.id where mg.goods_id = #{id}")
public GoodsVo getOneGoodsVoById(@Param("id")long goodsId);

}


猜你喜欢

转载自blog.csdn.net/jiuweideqixu/article/details/80443731
3-1