项目介绍
此项目主要基于HTTP协议实现,实现了最基本的收银台功能,使用Spring MVC框架,Mybatis,MySQL,HTML,CSS,JS等技术开发而成。
项目前端界面
后台界面
项目功能
使用框架
业务逻辑
注册
- 通过form表单发起post请求 静态资源
- 验证逻辑
POST/register.do
动态资源
-
Controller 读取from表单提交的用户信息,做参数的合法性校验
-
Service 负责密码加密的工作
-
Mapper 数据库的插入
Mybatis
实现
登录
login.html
通过form表单发起post请求,Controller
进行参数合法性校验,然后将name,password
通过Service
进行逻辑验证
功能实现
项目搭建
搭建Maven项目,导入Spring Web
,Mybatis Framework
,MySQL Drive
r,Thymeleaf
,Lombok
.这些依赖.
环境配置
环境配置主要是连接数据库,根据业务需要进行环境搭建
- 数据库连接
- 创建数据库
- 创建数据表
spring:
main:
banner-mode: off
log-startup-info: false
datasource:
url: jdbc:mysql://127.0.0.1:3306/MyDB?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: DBName
password: DBPassword
logging:
level:
root: warn
com.example: debug
mybatis:
mapper-locations: classpath:/mapper/**Mapper.xml
create database cash charset utf8mb4;
-- 创建用户表(主键、用户名(唯一)、密码)
create table cash.users (
user_id int primary key auto_increment,
username varchar(50) not null unique,
password char(60) not null
) comment '用户表,表示货管和收银两种角色,不区分';
-- 创建商品表(主键、关系字段、名称、介绍、库存、单位、价格、折扣)
create table cash.products (
product_id int primary key auto_increment,
user_id int not null comment '这是用户上架商品的一对多的关系字段',
name varchar(100) not null comment '名称',
introduce varchar(200) not null comment '介绍',
stock int not null comment '库存数量',
unit varchar(10) not null comment '显示单位',
price int not null comment '价格,单位是分,也就是 100 表示 1 块钱',
discount int not null comment '折扣,取值范围应该是 (0, 100]'
) comment '商品表';
-- 创建订单表(主键、关系字段、订单编号(uuid)、下单时间、完成时间、状态)
create table cash.orders (
order_id int primary key auto_increment,
user_id int not null comment '这个是用户创建订单的一对多的关系字段',
uuid char(32) not null unique comment '对外显示的不连续的订单编号,不会重复',
created_at datetime not null comment '订单的下单时间',
finished_at datetime null default null comment '订单的完成时间',
payable int not null comment '本次订单的应付金额,单位是分,提升显示方便性做的冗余字段',
actual int not null comment '本次订单的实付金额,单位是分,提升显示方便性做的冗余字段',
status int not null comment '进行中(1) | 已完成(2)'
) comment '订单的总体信息';
-- 创建订单和商品的关系表 —— 订单项表(主键、两个关系字段、为了防止商品下架后信息丢失,将商品信息备份一份)
create table cash.order_items (
id int primary key auto_increment,
order_id int not null comment '属于哪个订单',
product_id int not null comment '订单中的哪个商品',
-- 商品的冗余备份
product_name varchar(100) not null,
product_introduce varchar(200) not null,
product_number int not null comment '本次订单购买这个商品多少份',
product_unit varchar(10) not null,
product_price int not null,
product_discount int not null
) comment '订单项,记录本次订单的每一份商品信息';
商品管理
- 商品上架
通过form表单发起post请求,然后Controller层进行表单读取,并做合法性的参数校验,Service负责将商品信息映射为一个对象,通过Mapper层使用Mybatis
将数据插入 - 商品浏览
prduct/list.html
提供HTML的框架
product/list.js
发起ajax
请求
product/list.json
,以json
的格式返回 - 商品更新
product/update.html
通过form表单发起post请求
Controller
进行登录校验
Service层
并将要修改的商品构造为一个对象,通过Mybatis完成数据的更新
订单管理
- 创建订单
-
通过
product/creat.html
form表单填写1-2,1-3
这种信息,通过Controller解析并映射为Map<ID,数量>这种形式 -
Service层的作用:
查询库存是否足够
遍历每个商品,进行减库存操作
创建订单信息,生成订单
通过Controller
层重定向到商品订单详情页面order/detail
-
浏览器收到了HTTP 重定向响应,继续发送新的地址(发起对
/order/detail/{uuid}
的GET
请求) -
后端工作:
Controller
:解析uuid、判断用户是否登录
Service
:根据uuid
查询订单基本信息(OrderMapper)、根据orderld
查询所有相关订单项信息(OrderitemMapper)
Controller: MVC流程中,将查询到的订单信息作为order
放到model
中
将资源resources/templates/order-detail.html
作为View
Spring(配合Thymeleaf)将Model和View结合起来,生成最终的HTML内容,响应给前端 -
浏览器收到响应:用户看到了最终的该订单的详情信息
项目测试
对商品上架,商品浏览,商品下架,页面跳转进行了单元测试
对项目设计了测试用例
测试用例
项目总结
- 改进项
- 为超市老板增加统计功能(商品,销售额等)
- 进行权限控制,不同的角色功能不同
- 参数合法性校验,更新商品部需要再全部输入,使用扫描枪完成对商品条码的扫描输入