赞
踩
随着移动支付像微信和支付宝的普及,更多的人不愿意在口袋里放现金,导致本人捡钱的机会大大的降低,当前2021年随着美团和饿了么外卖平台的普及,更多的人不愿意花大量的时间在家自己煮饭吃,宁愿节约时间,甚至有的是不愿意洗碗,都在平台上点外卖,但是目前的两家外卖巨头,平台抽取商家的利润实在是收费太高,让大家很不情愿在上面入驻。
本设计是一个基于java采用springboot+mybatis框架开发的网上点餐项目,本在线点餐网站以IDEA+mysql为开发环境,HTML+CSS+JavaScript为页面编辑语言,springboot+mybatis为后台编辑语言来设计开发,开发一个在线点餐正是为了企业独立开发的目的。在国外 J2EE 已经成为开发电子商务平台的主流技术,本文研发的在线点餐系统使消费者进行购物时,不但菜品推荐更为符合食客的消费习惯,且菜品的质量安全以及消费者个人信息能够得到有效保障。
1)展示菜单:按不同的分类(如主食,小炒,饮料)来显示一些菜品信息
2)新品上架:最新上架的菜品有“新”标识
3)推荐星值:1-5星
1)用户注册:要求在客户端需要作一些格式验证,如果用户名已经存在,则要提示用户该账号已存在,注册失败!
2)用户登录:登录成功跳转到首页
A、用户在首页添加菜品到购物车
B、用户进入购物车还可以进行动态删减数量
C、用户提交订单,清空购物车
D、用户可以实时看到订单的状态(未接单、已接单(届时送达)、已完结)
E、用户可以查看并修改自己的信息(头像、密码、姓名、手机、地址)
项目pom.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.guzhz</groupId>
- <artifactId>springboot_orderfood_online</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springboot_orderfood_online</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
-
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.11.1</version>
- </dependency>
-
- <!--阿里巴巴druid-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.22</version>
- </dependency>
-
- <!--整合swagger-->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.9.2</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.9.2</version>
- </dependency>
-
- <!--MybatisPlus-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.3.1</version>
- </dependency>
- <!--代码生成器-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>3.3.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity-engine-core</artifactId>
- <version>2.2</version>
- </dependency>
-
- <!--安全策略-->
- <dependency>
- <groupId>org.thymeleaf.extras</groupId>
- <artifactId>thymeleaf-extras-springsecurity5</artifactId>
- <version>3.0.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintage</groupId>
- <artifactId>junit-vintage-engine</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
-
- <build>
-
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
-
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- <repositories>
- <repository>
- <id>maven-ali</id>
- <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- <updatePolicy>always</updatePolicy>
- <checksumPolicy>fail</checksumPolicy>
- </snapshots>
- </repository>
- </repositories>
- </project>
数据库文件:
- DROP TABLE IF EXISTS `menu_detail`;
- CREATE TABLE `menu_detail` (
- `md_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜品id',
- `md_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '菜品名称',
- `md_price` int(10) NULL DEFAULT NULL COMMENT '菜品价格',
- `md_amount` int(10) NULL DEFAULT NULL COMMENT '菜品数量',
- `md_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '/images/no_Image_default.jpg' COMMENT '菜品图片',
- `md_new` int(10) NULL DEFAULT 1 COMMENT '是否为新产品:默认为是1,否0',
- `md_star` int(10) NULL DEFAULT 3 COMMENT '推荐星值,默认为3; 0-5',
- `md_deleted` int(10) NULL DEFAULT 0 COMMENT '逻辑删除',
- `md_version` int(10) NULL DEFAULT 1 COMMENT '乐观锁',
- `mt_id` int(10) NULL DEFAULT NULL COMMENT '类型id',
- PRIMARY KEY (`md_id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of menu_detail
- -- ----------------------------
- INSERT INTO `menu_detail` VALUES (1, '白饭', 2, 998, '/images/439b73708eb0431c8d41c2102f910f87.jpg', 0, 3, 0, 44, 1);
- INSERT INTO `menu_detail` VALUES (2, '炒河粉', 15, 998, '/images/172b1faf39db4944a7a1f7bacb1296ef.jpg', 1, 5, 0, 8, 2);
- INSERT INTO `menu_detail` VALUES (3, '大馒头10个', 10, 997, '/images/fbb8786ea77e414ca9c67ddb83a303bf.jpg', 1, 4, 0, 11, 1);
- INSERT INTO `menu_detail` VALUES (4, '扬州炒饭', 15, 999, '/images/590b37b1bd494afaa45b7a712bf191c9.jpg', 1, 5, 0, 6, 1);
- INSERT INTO `menu_detail` VALUES (5, '玉米粥', 9, 999, '/images/0d4a7a6e70614b5287f586b4ece1cf61.jpg', 1, 4, 0, 6, 1);
- INSERT INTO `menu_detail` VALUES (6, '巨无霸', 30, 999, '/images/5b3fb289d524443fb53217fae93d4a5b.jpg', 1, 5, 0, 3, 4);
- INSERT INTO `menu_detail` VALUES (7, '益禾蜂蜜拿铁', 10, 999, '/images/1aac7ef93f7b45cab7d98fa0c6c9d19f.jpg', 1, 4, 0, 6, 3);
- INSERT INTO `menu_detail` VALUES (8, '烧鸡翅', 35, 999, '/images/7c03668169f2406192a16cc47c9577dd.jpg', 0, 5, 0, 2, 2);
- INSERT INTO `menu_detail` VALUES (9, '芹菜炒肉', 30, 999, '/images/5f83429ba92349aa9a520f822d85202a.jpg', 1, 4, 0, 2, 2);
- INSERT INTO `menu_detail` VALUES (10, '煎蛋', 15, 999, '/images/bef255b63555472bad0111f109e2662e.jpg', 1, 3, 0, 2, 2);
- INSERT INTO `menu_detail` VALUES (11, '瑞幸小鹿茶', 27, 992, '/images/a3a4c26ad4dd4715ba9775f6b34de639.jpg', 1, 5, 0, 4, 3);
- INSERT INTO `menu_detail` VALUES (12, '一点点奶茶', 20, 999, '/images/2cdf1fcbe39c408492fdaf5d3d2f1f67.jpg', 1, 5, 0, 2, 3);
- INSERT INTO `menu_detail` VALUES (13, '星巴克咖啡', 35, 999, '/images/999713e493d743f1bb03ec8921201d6d.jpg', 0, 2, 0, 2, 3);
- INSERT INTO `menu_detail` VALUES (14, '通心菜', 15, 999, '/images/5559f8b80c4548a6aeaf19e1fafdbff8.jpg', 1, 4, 0, 2, 2);
- INSERT INTO `menu_detail` VALUES (15, '木耳菜花', 15, 999, '/images/83924f23a4cc4e1285f5833a6ec7f51b.jpg', 1, 4, 0, 2, 2);
-
- -- ----------------------------
- -- Table structure for menu_type
- -- ----------------------------
- DROP TABLE IF EXISTS `menu_type`;
- CREATE TABLE `menu_type` (
- `mt_id` int(11) NOT NULL AUTO_INCREMENT,
- `mt_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
- `mt_deleted` int(10) NULL DEFAULT 0,
- `mt_version` int(10) NULL DEFAULT 1,
- PRIMARY KEY (`mt_id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of menu_type
- -- ----------------------------
- INSERT INTO `menu_type` VALUES (1, '主食', 0, 1);
- INSERT INTO `menu_type` VALUES (2, '小炒', 0, 1);
- INSERT INTO `menu_type` VALUES (3, '饮品', 0, 1);
- INSERT INTO `menu_type` VALUES (4, 'KFC系列', 0, 1);
-
- -- ----------------------------
- -- Table structure for order_detail
- -- ----------------------------
- DROP TABLE IF EXISTS `order_detail`;
- CREATE TABLE `order_detail` (
- `od_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '订单id',
- `od_no` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '订单号',
- `od_createTime` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
- `od_detail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '详细内容',
- `od_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '姓名',
- `od_phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机',
- `od_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '地址',
- `od_total` int(10) NULL DEFAULT NULL COMMENT '成交价',
- `od_remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
- `od_status` int(10) NULL DEFAULT NULL COMMENT '订单状态',
- `u_id` int(10) NULL DEFAULT NULL COMMENT '用户id',
- PRIMARY KEY (`od_id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of order_detail
- -- ----------------------------
- INSERT INTO `order_detail` VALUES (17, '20200701124007326', '2022-03-01 12:40:07', '【白饭 × 1;大馒头10个 × 1;炒河粉 × 1;瑞幸小鹿茶 × 1;】', '内测用户', '13544551200', '广东广州', 54, '内测用户测试', 2, 2);
- INSERT INTO `order_detail` VALUES (18, '20200701195218732', '2022-03-01 19:52:19', '【瑞幸小鹿茶 × 6;】', 'xxx', '13544551200', '广东广州', 162, '123', 2, 2);
- INSERT INTO `order_detail` VALUES (19, '20200702010523918', '2022-03-02 01:05:23', '【大馒头10个 × 1;】', '内测用户', '13544551200', '广东广州', 10, '多加点糖', 2, 2);
-
- -- ----------------------------
- -- Table structure for shopping_cart
- -- ----------------------------
- DROP TABLE IF EXISTS `shopping_cart`;
- CREATE TABLE `shopping_cart` (
- `sc_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '购物车序号',
- `sc_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '菜品名称',
- `sc_amount` int(10) NULL DEFAULT NULL COMMENT '单品数量',
- `sc_price` int(10) NULL DEFAULT NULL COMMENT '菜品价格',
- `sc_img` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '菜品图片',
- `md_id` int(10) NULL DEFAULT NULL COMMENT '菜品id',
- `u_id` int(10) NULL DEFAULT NULL COMMENT '当前用户id',
- PRIMARY KEY (`sc_id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 32 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of shopping_cart
- -- ----------------------------
- INSERT INTO `shopping_cart` VALUES (28, '白饭', 1, 2, '/images/439b73708eb0431c8d41c2102f910f87.jpg', 1, 1);
- INSERT INTO `shopping_cart` VALUES (31, '大馒头10个', 1, 10, '/images/fbb8786ea77e414ca9c67ddb83a303bf.jpg', 3, 2);
-
- -- ----------------------------
- -- Table structure for tb_user
- -- ----------------------------
- DROP TABLE IF EXISTS `tb_user`;
- CREATE TABLE `tb_user` (
- `u_id` int(5) NOT NULL AUTO_INCREMENT COMMENT '用户id',
- `u_username` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
- `u_password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
- `u_role` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '角色_0为商家_1为顾客',
- `u_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户姓名',
- `u_phone` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户手机',
- `u_address` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户地址',
- `u_money` int(100) NULL DEFAULT 0 COMMENT '用户收支',
- `u_url` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '/images/head_Image_default.jpg' COMMENT '头像',
- `u_deleted` int(1) NULL DEFAULT 0 COMMENT '逻辑删除:默认为0',
5.0.1 登录
配置springboot Security5,使用Security自带的登录请求,前端修改为自己设计的登录页面。springboot Security5实现接入数据库,托管账户。具体实现如下:
@Override //授权规则
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/","/index","/main").permitAll()
.antMatchers("/userInfo","/insertUserInfo","/edUserInfo","/deleteUser/**").hasAnyRole("admin")
.antMatchers("/menuManage","/deleteOneMenu/**","/upLoadImg","/insertMenu","/updateMenu").hasAnyRole("admin")
.antMatchers("/insertType","/updateType","/deleteType").hasAnyRole("admin")
.antMatchers("/orderManage","/changeStatusToOne").hasAnyRole("admin")
.antMatchers("/myInfo","/updateMyImg","/updateMyInfo").hasAnyRole("admin","user")
.antMatchers("/shoppingCart","/shoppingCart-plus","/shoppingCart-minus","/addShoppingCart").hasAnyRole("user")
.antMatchers("/toOrder","/myOrder","/changeStatusToTwo").hasAnyRole("user");
http.formLogin().loginPage("/toLogin").usernameParameter("uUsername").passwordParameter("uPassword")
.loginProcessingUrl("/login") //表单提交的请求,可以不需要路由(走security的路由)
.failureUrl("/toLogin?error=true");
http.csrf().disable(); //取消ajax拦截(post),如果改为post/get则可能不会报错
http.logout().logoutSuccessUrl("/toLogin");
//解决中文乱码问题
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8"); filter.setForceEncoding(true);
http.addFilterBefore(filter, CsrfFilter.class);
/*
//开启记住我功能
http.rememberMe().rememberMeParameter("remember");*/
}
userServiceImpl实现验证方法
@Override //验证规则
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(tbUserService).passwordEncoder(new NoPasswordEncoder());
}
4.1.2 注册
判断前端传过来的字段,查询数据库中是否存在,如果不存在则完成插入,否则提示该账号已存在。
@PostMapping("/register")
public String register(String uUsername, String uPassword, String uRole,Model model){
List<TbUser> users = tbUserService.selectAllUser();
for (TbUser temp : users){ //验证成功,直接登录
if(uUsername.equals(temp.getUUsername())){
model.addAttribute("msg","刚刚:注册失败,该用户已存在!");
return "user/login";
}
}
TbUser user = new TbUser();
user.setUUsername(uUsername);
user.setUPassword(uPassword);
user.setURole(uRole);
tbUserService.insertUser(user);
model.addAttribute("msg","刚刚:注册成功,赶快登录进行登录吧~");
return "user/login";
}
4.2.1 读取菜品数据
读取菜品数据表和菜品类型表,完成视图解析,返回到首页
@RequestMapping({"/main","/","/index"})
public String main(Model model,HttpSession session){
//用于封装整合后的对象的集合
List<MenuDetail> menus = new ArrayList<MenuDetail>();
List<MenuType> types = menuTypeService.selectAllType(); //查询出所有类型对象
//临时集合,用于存放需要移除的,0菜品的类型对象
List<MenuType> temp = new ArrayList<MenuType>();
//通过mt_id重新封装对象列表
for (MenuType type : types){
List<MenuDetail> menu = menuDetailService.selectByType(type.getMtId());
//如果 当前分类没有菜品,则移除该分类,同时不添加
if (menu.size() == 0){
temp.add(type);
}
menus.addAll(menu);
}
//封装后的list
model.addAttribute("menus",menus);
//移除空的类型
for (MenuType t : temp){
types.remove(t);
}
model.addAttribute("types",types);
return "main";
}
4.3.1 加入购物车
通过Jquery获得遍历出来的菜品,经过ajax发送请求到后端完成菜品插入到购物车数据库表中。
function getDetail(a) {
let list = $(a).children('input'); //获得当前元素下的所有子input
if (list.length == 5){ //5个说明处于未登录状态
alert("请先登录,再订餐哦~");
}
if($(list[6]).val() == "ROLE_admin"){
alert("你就是老板啦,你不能下单哦~");
} else{
let mdName = $(list[0]).val();
let mdPrice = $(list[2]).val();
$('#tips-name').html("【名称】"+mdName);
$('#tips-price').html("【价格】"+mdPrice+'元');
/*发起请求*/
let postData = {};
postData['scName'] = mdName;
postData['scAmount'] = 1;
postData['scPrice'] = mdPrice;
postData['scImg'] = $(list[3]).val();
postData['mdId'] = $(list[4]).val();
postData['uId'] = $(list[5]).val();
$.ajax({
url: "/addShoppingCart",
type: 'POST',
data: postData,
success: function (arg) {
console.log(arg);
// window.location.reload();
}
})
}
}
/*去我的购物车页面*/
@RequestMapping("/shoppingCart")
public String shoppingCart(Model model,int uId){
//查询购物车的商品
List<ShoppingCart> products = shoppingCartService.selectAllByUId(uId);
int total = 0;
for (ShoppingCart cart : products){
total += cart.getScPrice()*cart.getScAmount();
}
model.addAttribute("products",products);
model.addAttribute("total",total);
if(total>=30 && total<50){
model.addAttribute("paytotal",total-2);
}
if(total>=50 && total<80){
model.addAttribute("paytotal",total-4);
}
if(total>=80){
model.addAttribute("paytotal",total-10);
}
return "user/shoppingCart";
}
4.3.2 管理购物车
动态删减数量
@RequestMapping("/shoppingCart-plus")
public String shoppingCartPlus(int uId,int mdId){
//把数量+1
ShoppingCart one = shoppingCartService.selectOneByMdIdAndUId(mdId, uId);
one.setScAmount(one.getScAmount()+1);
shoppingCartService.updateProduct(one);
System.out.println("数量+1成功");
return "redirect:/shoppingCart?uId="+uId;
}
@RequestMapping("/shoppingCart-minus")
public String shoppingCartMinus(int uId,int mdId){
/*如果 数量== 1,直接删除*/
ShoppingCart one = shoppingCartService.selectOneByMdIdAndUId(mdId, uId);
if (one.getScAmount() == 1 ) {
//删除数据
shoppingCartService.deleteById(one.getScId());
return "redirect:/shoppingCart?uId="+uId;
}else{
//数量-1
one.setScAmount(one.getScAmount()-1);
shoppingCartService.updateProduct(one);
System.out.println("数量-1成功");
return "redirect:/shoppingCart?uId="+uId;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。