SpringBoot微信点餐系统(一)

项目设计

角色划分:

               卖家(PC端)   是一个卖家管理的后台
               买家(手机端) 是一个微信公众账号提供的服务

功能分析:

               包含三大模块,商品、订单、类目

               针对买家,商品模块有商品列表数据、订单模块有订单的创建、查询、取消等

               针对卖家,有订单管理、商品管理、类目管理等

                关系:

                        买家会先查询商品,然后创建/查询订单

                        卖家会查询订单/接单,同时也可以管理商品

                        买家与卖家之间通过消息互通

部署架构:

               微信端和pc端浏览器都通过nginx服务器来访问,如果请求的是后端接口,nginx服务器会进行转发,转发到tomcat(也就是我们的web服务),如果tomcat做了缓存,就会用到访问redis服务,如果没有做缓存,就会用到访问mysql,我们的应用是支持分布式部署的,也就是我们的tomcat是多台服务器,多个应用

架构和基础框架
数据库设计

五张表:类目表、订单主表、商品表、订单详情表、卖家信息表

商品表:(ID、名称、单价、库存、描述、图片链接)
类目表:(类目ID、类目名称,一个类目下有多个商品、每个商品又归属到类目,所以类目表与商品表是一对多的关系)
订单详情表:(存放订单具体内容:商品名称、商品个数)
订单主表:(订单的核心,包括买家的信息、订单总额、支付状态,它与订单详情表是一对多的关系)
                     例如一个订单里面买了两种商品,详情表里面会有两条记录
卖家信息表:(卖家账号、卖家密码,作为卖家后台管理的权限认证)

设计数据库

sell.sql 
商品表:(商品ID不用自增类型,如果商品多了不够用,其中商品价格是8位,末尾保留2个小数点,icon表示小图,图片其实是一个链接,所以我们用字符串来保存,其中创建时间的默认值是当前时间,更新时间时我们希望自动更新这个更新时间字段,由mysql数据库来完成,不需要我们写其他代码也就是下面的 on update current_timestamp,我们在查询商品的时候都是通过商品的ID来查,所以我们设置一个主键)


类目表:(由于类目不会像商品一样多,所以int类型足够了,并且自增,category_type为类目的编号,我们为什么不用类目的ID直接作为类目的编号呢?因为ID是自增的,而类目编号往往需要自定义,例如1为热榜,2为精选套餐,建议也加上创建时间和修改时间,主键是类目的ID,因为两个表都有类目编号,所以在查询的时候我们要进行一个性能的优化,因为我们希望类目编号唯一,不能出现两个相同的类目编号,所以要加一个约束,UNIQUE KEY的用途:主要是用来防止数据插入的时候重复的

@引用

一.主键:为了标识数据库记录唯一性,不允许记录重复,且键值不能为空,主键也是一个特殊索引。
二.相同点:Unique key与primarykey 约束都强调唯一性。
三.不同点:
1.一个表只能有一个PRIMARY KEY,但可以有多个UNIQUE KEY ;
2.unique key 约束只针对非主键列,允许有空值。Primary key 约束针对主键列,不允许有空值。
primary key = unique + not null
UNIQUE KEY的用途:主要是用来防止数据插入的时候重复的。
四.例子:防止重复插入数据,如防止name字段插入重复数据。

设置unique索引



订单主表:(订单主表的ID与商品ID类似,我们也不能用自增方式,其中openid就是买家的微信ID,订单状态默认为0,表示新下单,订单ID作为主键,查询时,依据此,查询某个人下了什么订单,就用openid来查询,所以加一个索引idx_buyer_openid)

 订单详情表:(我们查询详情时需要用订单主表的ID来进行查询,则我们将订单ID作为索引key `idx_order_id` (`order_id`))

create table `product_info` (
   `product_id` varchar(32) not null,
   `product_name` varchar(64) not null comment '商品名称',
   `product_price` decimal(8,2) not null comment '单价',
   `product_stock` int not null comment '库存',
   `product_description` varchar(64) comment '描述',
   `product_icon` varchar(512) comment '小图',
   `category_type` int not null comment '类目编号',
   `create_time` timestamp not null default current_timestamp comment '创建时间',
   `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
   primary key (`product_id`)
) comment '商品表';

create table `product_category` (
   `category_id` int not null auto_increment,
   `category_name` varchar(64) not null comment '类目名称',
   `category_type` int not null comment '类目编号',
   `create_time` timestamp not null default current_timestamp comment '创建时间',
   `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
   primary key (`category_id`),
   unique key `uqe_category_type` (`category_type`)
) comment '类目表';

create table `order_master` (
   `order_id` varchar(32) not null,
   `buyer_name` varchar(32) not null comment '买家名称',
   `buyer_phone` varchar(32) not null comment '买家电话',
   `buyer_address` varchar(128) not null comment '买家地址',
   `buyer_openid` varchar(64) not null comment '买家微信openid',
   `order_amount` decimal(8,2) not null comment '订单总金额',
   `order_status` tinyint(3) not null default '0' comment '订单状态,默认0,新下单',
   `pay_status` tinyint(3) not null default '0' comment '支付状态,默认0,未支付',
   `create_time` timestamp not null default current_timestamp comment '创建时间',
   `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
   primary key (`order_id`),
   key `idx_buyer_openid` (`buyer_openid`)
) comment '订单主表';

create table `order_detail` (
   `detail_id` varchar(32) not null,
   `order_id` varchar(32) not null,
   `product_id` varchar(32) not null,
   `product_name` varchar(64) not null comment '商品名称',
   `product_price` decimal(8,2) not null comment '单价',
   `product_quantity` int not null comment '商品数量',
   `product_icon` varchar(512) comment '小图',
   `create_time` timestamp not null default current_timestamp comment '创建时间',
   `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
   primary key (`detail_id`),
   key `idx_order_id` (`order_id`)
) comment '订单详情表';

 注意:如果是mysql5.6及之前,则设置两个字段default current_timestamp  是报错的,5.7以后不报错
            我们这里是5.7
我们可以把这个创建表的sql,保存到createtable.sql文件中,后续使用

猜你喜欢

转载自blog.csdn.net/dqxiaoxiao/article/details/82772752