1:在企业级中,由于数据量是比较大的,所以id一般采用的是varchar,不采用int
2:凡是涉及到价格的统一采用decimal,例如本项目中单价如下: product_price
decimal(8,2) not null comment ‘单价’, 表示具有8位整数,以及两位小数,总共10位。
3:因为库存需要计算(加减),所以此处设置为int类型,商品表中库存如下:product_stock
int not null comment ‘库存’,
4:商品图片,在数据库中一般以链接的形式存在,所以采用varchar类型(注意,在数据库中字段长度一般都是2的n次方):product_icon
varchar(512) comment ‘小图’,
5:创建时间默认为当前时间: create_time
timestamp not null default current_timestamp comment ‘创建时间’,
6:设置商品的状态:product_status
tinyint(3) DEFAULT ‘0’ COMMENT ‘商品状态,0正常1下架’,
1:因为商品分类是有限的,所以我这里直接采用int类型就已经足够了: category_id
int not null auto_increment,
2:一般而言,均有创建时间和更新时间: 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
)
1:订单表和商品表的id都是一样的采用字符类型
2:订单状态一般都是比较少的,所以此处需要tinyint类型就可以了的: order_status
tinyint(3) not null default ‘0’ comment ‘订单状态, 默认为新下单’,
3:支付状态和订单状态是一样的
4:订单表加索引: key idx_buyer_openid
(buyer_openid
)
1:同上
– 类目
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
)
);
– 商品
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 ‘小图’,
product_status
tinyint(3) DEFAULT ‘0’ COMMENT ‘商品状态,0正常1下架’,
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
)
);
– 订单
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 ‘订单状态, 默认为新下单’,
pay_status
tinyint(3) not null default ‘0’ 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 (order_id
),
key idx_buyer_openid
(buyer_openid
)
);
– 订单商品
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
)
);
– 卖家(登录后台使用, 卖家登录之后可能直接采用微信扫码登录,不使用账号密码)
create table seller_info
(
id
varchar(32) not null,
username
varchar(32) not null,
password
varchar(32) not null,
openid
varchar(64) not null comment ‘微信openid’,
create_time
timestamp not null default current_timestamp comment ‘创建时间’,
update_time
timestamp not null default current_timestamp on update current_timestamp comment ‘修改时间’,
primary key (id
)
) comment ‘卖家信息表’;
1:ProductCategory类
注解@Entity是把数据库对象映射成属性;
@Id注解表示id是主键,@GeneratedValue表示主键id是自增类型;
@Data 注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法等等,它用在springboot中可以减少一些步必要代码;需要在pom.xml里面进行配置依赖:
org.projectlombok
lombok
@DynamicUpdate注解表示的是更新变化的字段,而不是更新指定的字段。在Spring Data JPA 中要注意@DynamicInsert和@DynamicUpdate。
@DynamicInsert上设置为true表示insert对象的时候,生成动态的insert语句,如果这个字段的值是null就不会加入到insert语句当中,默认false。
@DynamicUpdate设置为true,表示的是update对象的时候,生成动态的update语句,如果这个字段的值是null就不会被加入到update语句中,默认false。比如说,我们修改了商品,那么系统中就会显示数据修改的时间。
补充:
1:注解
@Getter/@Setter : 注解在类上, 为类提供读写属性 @ToString : 注解在类上, 为类提供 toString() 方法
@Slf4j : 注解在类上, 为类提供一个属性名为 log 的 log4j 的日志对象
@Log4j : 注解在类上, 为类提供一个属性名为 log 的 log4j 的日志对象
2:SpringData JPA
(1)什么是SpringData JPA?
SpringData是一个用于简化数据库访问,支持云服务的开源框架。目标是使数据库访问变得方便快捷。公司的ORM框架,均采用SpringData JPA。SpringData是数据库开源框架,包含对关系数据库,非关系数据库,云数据服务访问支持等。
JPA的全称是Java Persistence API,Persistence 是持久化的意思。所以,中文全称是【JAVA对象持久化的 API】。简单来说,可以理解为是一种JAVA的标准规范,这个规范为JAVA对象的持久化制定了一些标准的接口。
要注意的是,JPA只是一个接口规范,而不是实现。具体实现由各供应商来完成,例如Hibernate,TopLink,OpenJPA都很好地实现了JPA接口。
(2)Hibernate和JPA的关系
JPA是Hibernate的一个抽象,提供 一些编程的API接口,但具体实现则由ORM厂商提供实现
(3)application.yml中配置jpa
jpa:
#将数据库打印出来
show-sql: true
表示将数据库给打印出来。