当前位置:   article > 正文

基于springboot在线网上点餐平台设计与实现_基于springboot的外卖点餐

基于springboot的外卖点餐

随着移动支付像微信和支付宝的普及,更多的人不愿意在口袋里放现金,导致本人捡钱的机会大大的降低,当前2021年随着美团和饿了么外卖平台的普及,更多的人不愿意花大量的时间在家自己煮饭吃,宁愿节约时间,甚至有的是不愿意洗碗,都在平台上点外卖,但是目前的两家外卖巨头,平台抽取商家的利润实在是收费太高,让大家很不情愿在上面入驻。

本设计是一个基于java采用springboot+mybatis框架开发的网上点餐项目,本在线点餐网站以IDEA+mysql为开发环境,HTML+CSS+JavaScript为页面编辑语言springboot+mybatis为后台编辑语言来设计开发,开发一个在线点餐正是为了企业独立开发的目的。在国外 J2EE 已经成为开发电子商务平台的主流技术本文研发的在线点餐系统使消费者进行购物时,不但菜品推荐更为符合食客的消费习惯,且菜品的质量安全以及消费者个人信息能够得到有效保障

 

  1. 前台总体功能

  1. 首页功能:

1)展示菜单:按不同的分类(如主食,小炒,饮料)来显示一些菜品信息

2)新品上架:最新上架的菜品有“新”标识

3)推荐星值:1-5星

  1. 用户(顾客)功能:

1)用户注册:要求在客户端需要作一些格式验证,如果用户名已经存在,则要提示用户该账号已存在,注册失败!

2)用户登录:登录成功跳转到首页

A、用户在首页添加菜品到购物车 

B、用户进入购物车还可以进行动态删减数量

C、用户提交订单,清空购物车

D、用户可以实时看到订单的状态(未接单、已接单(届时送达)、已完结)

E、用户可以查看并修改自己的信息(头像、密码、姓名、手机、地址)

项目pom.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.guzhz</groupId>
  12. <artifactId>springboot_orderfood_online</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot_orderfood_online</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>com.fasterxml.jackson.core</groupId>
  22. <artifactId>jackson-databind</artifactId>
  23. <version>2.11.1</version>
  24. </dependency>
  25. <!--阿里巴巴druid-->
  26. <dependency>
  27. <groupId>com.alibaba</groupId>
  28. <artifactId>druid</artifactId>
  29. <version>1.1.22</version>
  30. </dependency>
  31. <!--整合swagger-->
  32. <dependency>
  33. <groupId>io.springfox</groupId>
  34. <artifactId>springfox-swagger2</artifactId>
  35. <version>2.9.2</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>io.springfox</groupId>
  39. <artifactId>springfox-swagger-ui</artifactId>
  40. <version>2.9.2</version>
  41. </dependency>
  42. <!--MybatisPlus-->
  43. <dependency>
  44. <groupId>com.baomidou</groupId>
  45. <artifactId>mybatis-plus-boot-starter</artifactId>
  46. <version>3.3.1</version>
  47. </dependency>
  48. <!--代码生成器-->
  49. <dependency>
  50. <groupId>com.baomidou</groupId>
  51. <artifactId>mybatis-plus-generator</artifactId>
  52. <version>3.3.2</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.apache.velocity</groupId>
  56. <artifactId>velocity-engine-core</artifactId>
  57. <version>2.2</version>
  58. </dependency>
  59. <!--安全策略-->
  60. <dependency>
  61. <groupId>org.thymeleaf.extras</groupId>
  62. <artifactId>thymeleaf-extras-springsecurity5</artifactId>
  63. <version>3.0.3.RELEASE</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-starter-security</artifactId>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.springframework.boot</groupId>
  75. <artifactId>spring-boot-starter-web</artifactId>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.springframework.boot</groupId>
  79. <artifactId>spring-boot-devtools</artifactId>
  80. <scope>runtime</scope>
  81. <optional>true</optional>
  82. </dependency>
  83. <dependency>
  84. <groupId>mysql</groupId>
  85. <artifactId>mysql-connector-java</artifactId>
  86. <scope>runtime</scope>
  87. </dependency>
  88. <dependency>
  89. <groupId>org.projectlombok</groupId>
  90. <artifactId>lombok</artifactId>
  91. <optional>true</optional>
  92. </dependency>
  93. <dependency>
  94. <groupId>org.springframework.boot</groupId>
  95. <artifactId>spring-boot-starter-test</artifactId>
  96. <scope>test</scope>
  97. <exclusions>
  98. <exclusion>
  99. <groupId>org.junit.vintage</groupId>
  100. <artifactId>junit-vintage-engine</artifactId>
  101. </exclusion>
  102. </exclusions>
  103. </dependency>
  104. </dependencies>
  105. <build>
  106. <resources>
  107. <resource>
  108. <directory>src/main/java</directory>
  109. <includes>
  110. <include>**/*.xml</include>
  111. </includes>
  112. <filtering>false</filtering>
  113. </resource>
  114. </resources>
  115. <plugins>
  116. <plugin>
  117. <groupId>org.springframework.boot</groupId>
  118. <artifactId>spring-boot-maven-plugin</artifactId>
  119. </plugin>
  120. </plugins>
  121. </build>
  122. <repositories>
  123. <repository>
  124. <id>maven-ali</id>
  125. <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
  126. <releases>
  127. <enabled>true</enabled>
  128. </releases>
  129. <snapshots>
  130. <enabled>true</enabled>
  131. <updatePolicy>always</updatePolicy>
  132. <checksumPolicy>fail</checksumPolicy>
  133. </snapshots>
  134. </repository>
  135. </repositories>
  136. </project>

数据库文件:

  1. DROP TABLE IF EXISTS `menu_detail`;
  2. CREATE TABLE `menu_detail` (
  3. `md_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜品id',
  4. `md_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '菜品名称',
  5. `md_price` int(10) NULL DEFAULT NULL COMMENT '菜品价格',
  6. `md_amount` int(10) NULL DEFAULT NULL COMMENT '菜品数量',
  7. `md_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '/images/no_Image_default.jpg' COMMENT '菜品图片',
  8. `md_new` int(10) NULL DEFAULT 1 COMMENT '是否为新产品:默认为是1,否0',
  9. `md_star` int(10) NULL DEFAULT 3 COMMENT '推荐星值,默认为3; 0-5',
  10. `md_deleted` int(10) NULL DEFAULT 0 COMMENT '逻辑删除',
  11. `md_version` int(10) NULL DEFAULT 1 COMMENT '乐观锁',
  12. `mt_id` int(10) NULL DEFAULT NULL COMMENT '类型id',
  13. PRIMARY KEY (`md_id`) USING BTREE
  14. ) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  15. -- ----------------------------
  16. -- Records of menu_detail
  17. -- ----------------------------
  18. INSERT INTO `menu_detail` VALUES (1, '白饭', 2, 998, '/images/439b73708eb0431c8d41c2102f910f87.jpg', 0, 3, 0, 44, 1);
  19. INSERT INTO `menu_detail` VALUES (2, '炒河粉', 15, 998, '/images/172b1faf39db4944a7a1f7bacb1296ef.jpg', 1, 5, 0, 8, 2);
  20. INSERT INTO `menu_detail` VALUES (3, '大馒头10个', 10, 997, '/images/fbb8786ea77e414ca9c67ddb83a303bf.jpg', 1, 4, 0, 11, 1);
  21. INSERT INTO `menu_detail` VALUES (4, '扬州炒饭', 15, 999, '/images/590b37b1bd494afaa45b7a712bf191c9.jpg', 1, 5, 0, 6, 1);
  22. INSERT INTO `menu_detail` VALUES (5, '玉米粥', 9, 999, '/images/0d4a7a6e70614b5287f586b4ece1cf61.jpg', 1, 4, 0, 6, 1);
  23. INSERT INTO `menu_detail` VALUES (6, '巨无霸', 30, 999, '/images/5b3fb289d524443fb53217fae93d4a5b.jpg', 1, 5, 0, 3, 4);
  24. INSERT INTO `menu_detail` VALUES (7, '益禾蜂蜜拿铁', 10, 999, '/images/1aac7ef93f7b45cab7d98fa0c6c9d19f.jpg', 1, 4, 0, 6, 3);
  25. INSERT INTO `menu_detail` VALUES (8, '烧鸡翅', 35, 999, '/images/7c03668169f2406192a16cc47c9577dd.jpg', 0, 5, 0, 2, 2);
  26. INSERT INTO `menu_detail` VALUES (9, '芹菜炒肉', 30, 999, '/images/5f83429ba92349aa9a520f822d85202a.jpg', 1, 4, 0, 2, 2);
  27. INSERT INTO `menu_detail` VALUES (10, '煎蛋', 15, 999, '/images/bef255b63555472bad0111f109e2662e.jpg', 1, 3, 0, 2, 2);
  28. INSERT INTO `menu_detail` VALUES (11, '瑞幸小鹿茶', 27, 992, '/images/a3a4c26ad4dd4715ba9775f6b34de639.jpg', 1, 5, 0, 4, 3);
  29. INSERT INTO `menu_detail` VALUES (12, '一点点奶茶', 20, 999, '/images/2cdf1fcbe39c408492fdaf5d3d2f1f67.jpg', 1, 5, 0, 2, 3);
  30. INSERT INTO `menu_detail` VALUES (13, '星巴克咖啡', 35, 999, '/images/999713e493d743f1bb03ec8921201d6d.jpg', 0, 2, 0, 2, 3);
  31. INSERT INTO `menu_detail` VALUES (14, '通心菜', 15, 999, '/images/5559f8b80c4548a6aeaf19e1fafdbff8.jpg', 1, 4, 0, 2, 2);
  32. INSERT INTO `menu_detail` VALUES (15, '木耳菜花', 15, 999, '/images/83924f23a4cc4e1285f5833a6ec7f51b.jpg', 1, 4, 0, 2, 2);
  33. -- ----------------------------
  34. -- Table structure for menu_type
  35. -- ----------------------------
  36. DROP TABLE IF EXISTS `menu_type`;
  37. CREATE TABLE `menu_type` (
  38. `mt_id` int(11) NOT NULL AUTO_INCREMENT,
  39. `mt_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  40. `mt_deleted` int(10) NULL DEFAULT 0,
  41. `mt_version` int(10) NULL DEFAULT 1,
  42. PRIMARY KEY (`mt_id`) USING BTREE
  43. ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  44. -- ----------------------------
  45. -- Records of menu_type
  46. -- ----------------------------
  47. INSERT INTO `menu_type` VALUES (1, '主食', 0, 1);
  48. INSERT INTO `menu_type` VALUES (2, '小炒', 0, 1);
  49. INSERT INTO `menu_type` VALUES (3, '饮品', 0, 1);
  50. INSERT INTO `menu_type` VALUES (4, 'KFC系列', 0, 1);
  51. -- ----------------------------
  52. -- Table structure for order_detail
  53. -- ----------------------------
  54. DROP TABLE IF EXISTS `order_detail`;
  55. CREATE TABLE `order_detail` (
  56. `od_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '订单id',
  57. `od_no` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '订单号',
  58. `od_createTime` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  59. `od_detail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '详细内容',
  60. `od_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '姓名',
  61. `od_phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机',
  62. `od_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '地址',
  63. `od_total` int(10) NULL DEFAULT NULL COMMENT '成交价',
  64. `od_remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
  65. `od_status` int(10) NULL DEFAULT NULL COMMENT '订单状态',
  66. `u_id` int(10) NULL DEFAULT NULL COMMENT '用户id',
  67. PRIMARY KEY (`od_id`) USING BTREE
  68. ) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  69. -- ----------------------------
  70. -- Records of order_detail
  71. -- ----------------------------
  72. INSERT INTO `order_detail` VALUES (17, '20200701124007326', '2022-03-01 12:40:07', '【白饭 × 1;大馒头10个 × 1;炒河粉 × 1;瑞幸小鹿茶 × 1;】', '内测用户', '13544551200', '广东广州', 54, '内测用户测试', 2, 2);
  73. INSERT INTO `order_detail` VALUES (18, '20200701195218732', '2022-03-01 19:52:19', '【瑞幸小鹿茶 × 6;】', 'xxx', '13544551200', '广东广州', 162, '123', 2, 2);
  74. INSERT INTO `order_detail` VALUES (19, '20200702010523918', '2022-03-02 01:05:23', '【大馒头10个 × 1;】', '内测用户', '13544551200', '广东广州', 10, '多加点糖', 2, 2);
  75. -- ----------------------------
  76. -- Table structure for shopping_cart
  77. -- ----------------------------
  78. DROP TABLE IF EXISTS `shopping_cart`;
  79. CREATE TABLE `shopping_cart` (
  80. `sc_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '购物车序号',
  81. `sc_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '菜品名称',
  82. `sc_amount` int(10) NULL DEFAULT NULL COMMENT '单品数量',
  83. `sc_price` int(10) NULL DEFAULT NULL COMMENT '菜品价格',
  84. `sc_img` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '菜品图片',
  85. `md_id` int(10) NULL DEFAULT NULL COMMENT '菜品id',
  86. `u_id` int(10) NULL DEFAULT NULL COMMENT '当前用户id',
  87. PRIMARY KEY (`sc_id`) USING BTREE
  88. ) ENGINE = InnoDB AUTO_INCREMENT = 32 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  89. -- ----------------------------
  90. -- Records of shopping_cart
  91. -- ----------------------------
  92. INSERT INTO `shopping_cart` VALUES (28, '白饭', 1, 2, '/images/439b73708eb0431c8d41c2102f910f87.jpg', 1, 1);
  93. INSERT INTO `shopping_cart` VALUES (31, '大馒头10个', 1, 10, '/images/fbb8786ea77e414ca9c67ddb83a303bf.jpg', 3, 2);
  94. -- ----------------------------
  95. -- Table structure for tb_user
  96. -- ----------------------------
  97. DROP TABLE IF EXISTS `tb_user`;
  98. CREATE TABLE `tb_user` (
  99. `u_id` int(5) NOT NULL AUTO_INCREMENT COMMENT '用户id',
  100. `u_username` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
  101. `u_password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
  102. `u_role` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '角色_0为商家_1为顾客',
  103. `u_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户姓名',
  104. `u_phone` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户手机',
  105. `u_address` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户地址',
  106. `u_money` int(100) NULL DEFAULT 0 COMMENT '用户收支',
  107. `u_url` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '/images/head_Image_default.jpg' COMMENT '头像',
  108. `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 首页展示

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 消费者功能

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;

        }

    }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/555422
推荐阅读
相关标签
  

闽ICP备14008679号