基于Springboot外卖系统12:删除菜品套餐类别+修改套餐类别信息

简介: 在分类管理列表页面,可以对某个分类进行删除操作。需要注意的是当分类关联了菜品或者套餐时,此分类不允许删除。

1. 删除分类


1.1 需求分析


在分类管理列表页面,可以对某个分类进行删除操作。需要注意的是当分类关联了菜品或者套餐时,此分类不允许删除。


a3f715e468724315b719be3ea458b12a.png


1.2 前端页面分析


在前端页面中,点击 "删除" 按钮,就会触发定义的方法,然后往服务端发送异步请求,并传递参数id,执行删除分类操作。


ac09c0d57e1e406aaed5466a51958e1a.png


删除操作的具体执行流程如下:


1). 点击删除,页面发送ajax请求,将参数(id)提交到服务端


2). 服务端Controller接收页面提交的数据并调用Service删除数据


3). Service调用Mapper操作数据库


fcc9e072c2ff4a7ba67b037407d5d057.png


从上述的分析中,可以得到请求的信息如下:


请求 说明
请求方式 DELETE
请求路径 /category
请求参数 ?id=1395291114922618881


1.3 改进思路分析


删除分类数据需要检查删除的分类是否关联了菜品或者套餐,所以需要进行功能完善。完善后的逻辑为:


  • 根据当前分类的ID,查询该分类下是否存在菜品,如果存在,则提示错误信息


  • 根据当前分类的ID,查询该分类下是否存在套餐,如果存在,则提示错误信息


  • 执行正常的删除分类操作


那么在这里又涉及到两张表结构 dish(菜品表) 和 setmeal(套餐表)。具体的表结构如下:


ccc799fab91e4b5ea93e83fa730dbd43.png


738bc0b9384c414fb99964d6c86bc085.png


1.4 准备工作


1.4.1 Dish菜品实体类


package com.itheima.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
 菜品
 */
@Data
public class Dish implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    //菜品名称
    private String name;
    //菜品分类id
    private Long categoryId;
    //菜品价格
    private BigDecimal price;
    //商品码
    private String code;
    //图片
    private String image;
    //描述信息
    private String description;
    //0 停售 1 起售
    private Integer status;
    //顺序
    private Integer sort;
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    @TableField(fill = FieldFill.INSERT)
    private Long createUser;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;
}


1.4.2 Setmeal套餐实体类


package com.itheima.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
 * 套餐
 */
@Data
public class Setmeal implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    //分类id
    private Long categoryId;
    //套餐名称
    private String name;
    //套餐价格
    private BigDecimal price;
    //状态 0:停用 1:启用
    private Integer status;
    //编码
    private String code;
    //描述信息
    private String description;
    //图片
    private String image;
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    @TableField(fill = FieldFill.INSERT)
    private Long createUser;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;
}


1.4.3. Mapper接口DishMapper和SetmealMapper


DishMapper


package com.itheima.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.reggie.entity.Dish;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
}


SetmealMapper


package com.itheima.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.reggie.entity.Setmeal;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SetmealMapper extends BaseMapper<Setmeal> {
}


1.4.3 Service接口DishService和SetmealService


DishService


package com.itheima.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.reggie.entity.Dish;
public interface DishService extends IService<Dish> {
}


SetmealService


package com.itheima.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.reggie.entity.Setmeal;
public interface SetmealService extends IService<Setmeal> {
}


1.5 代码实现


1). 创建自定义异常


在业务逻辑操作过程中,如果遇到一些业务参数、操作异常的情况下,直接抛出此异常。


package com.itheima.reggie.common;
import javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec;
/**
 * Description: 自定义业务异常类
 * @date 2022/8/16 10:32
 */
public class CustomException extends RuntimeException{
    public CustomException(String message){
        super(message);
    }
}


2). 在CategoryService中扩展remove方法


package com.itheima.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.reggie.entity.Category;
public interface CategoryService extends IService<Category> {
    public void remove(Long id);
}


3). 在CategoryServiceImpl中实现remove方法


package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * Description: 类别业务层接口
 * @version 1.0
 * @date 2022/8/15 13:55
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService ;
    @Autowired
    private SetmealService setmealService;
    @Override
    /**@Description: 根据id删除分类,删除之前需要判定是否关联菜品
     * @version v1.0
     * @author LiBiGo
     * @date 2022/8/16 10:20
     */
    public void remove(Long id) {
        // A.查询当前分类是否关联菜品,关联则抛出业务异常类
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件,根据id查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0){
            // 已关联菜品,抛出异常
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }
        // B.查询当前分类是否关联套餐,关联则抛出业务异常类
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件,根据id查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2 > 0){
            // 已关联套餐,抛出异常
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }
        // 均无关联,则删除
        super.removeById(id);  // 父类实现了最基本的根据id查询的删除,所以直接调用就行
    }
}


那么在上述的业务逻辑中,当分类下关联的有菜品或者套餐时,在业务代码中抛出了自定义异常,会被异常处理器捕获,只需要在异常处理器中捕获这一类的异常,然后给页面返回对应的提示信息即可。


4). 在GlobalExceptionHandler中处理自定义异常


在全局异常处理器中增加方法,用于捕获自定义的异常 CustomException


package com.itheima.reggie.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.sql.SQLIntegrityConstraintViolationException;
/**
 * Description: 全局异常处理器
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})// 处理@RestController、@Controller的函数异常 //指定拦截那些类型的控制器;
@ResponseBody   //将方法的返回值 R 对象转换为json格式的数据, 响应给页面;
@Slf4j
public class GlobalExceptionHandler {
    /**@Description: 异常处理方法
     * @author LiBiGo
     * @date 2022/8/12 17:46
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class) // 处理指定异常类
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
        //Duplicate entry '299067' for key 'idx_username'
        if(ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split(" "); //提取重复字段,即哪个用户名重复 从0开始第2个即为用户名
            String msg = split[2] + "已存在";
            return R.error(msg);
        }
        return R.error("未知错误");
    }
    @ExceptionHandler(CustomException.class) // 处理自定义业务异常类
    public R<String> exceptionHandler(CustomException ex){
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }
}


5). 改造CategoryController的delete方法


注释掉原有的代码,在delete方法中直接调用categoryService中自定义的remove方法。


package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * Description: 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**@Description: 新增分类
     * @author LiBiGo
     * @date 2022/8/15 14:05
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        /**@Description: 分页查询
         * @author LiBiGo
         * @date 2022/8/15 14:21
         */
        // 分页构造
        Page<Category> pageinfo = new Page<>(page,pageSize);
        // 构造条件构造器对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        // 添加排序条件,根据sore进行排序
        queryWrapper.orderByAsc(Category::getSort);
        // 进行分页查询
        categoryService.page(pageinfo,queryWrapper);
        return R.success(pageinfo);
    }
    @DeleteMapping
    public R<String> delete(Long id){
        /**@Description: 根据id删除分类
         * @author LiBiGo
         * @date 2022/8/16 9:58
         */
        log.info("删除分类,id为{}",id);
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }
    @PutMapping
    public  R<String> update(@RequestBody Category category){
        /**@Description: 根据id修改分类信息
         * @author LiBiGo
         * @date 2022/8/16 10:49
         */
        log.info("根据id修改分类信息:{}",category);
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }
}


2. 修改分类


2.1 需求分析


在分类管理列表页面点击修改按钮,弹出修改窗口,在修改窗口回显分类信息并进行修改,最后点击确定按钮完成修改操作。


24ce70cb840b4966922a3f4497071179.png


2.2 前端页面分析


这里面大家会发现,修改功能我们还没有实现,但是当点击 "修改" 按钮的时候,我们并没有开发根据ID查询数据,进行页面回显的功能,但是页面的分类数据确实回显回来了。这是怎么做到的呢,我们来解析一下前端的代码实现(前端代码已经实现):


07ce80c27f914860bc07e67dc2a36e2f.png



那么回显这一步的操作前端已经实现,我们就只需要开发一个方法,修改操作的方法即可。我们可以通过浏览器来抓取一下修改操作的请求信息,如图:


a6119e90fe5b4f298ddccc72664201fd.png


具体的请求信息,整理如下:


请求 说明
请求方式 PUT
请求路径 /category
请求参数 {id: "1399923597874081794", name: "超值午餐", sort: 0}


2.3 代码实现


html页面中相关的代码都已经提供好了,我们已经分析了请求的信息,接下来就可以来创建服务端的CategoryController方法update方法。


【见上一个代码块,已集成】

目录
相关文章
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
JavaScript Java 关系型数据库
基于springboot的古树名木保护管理系统
本研究针对古树保护面临的严峻挑战,构建基于Java、Vue、MySQL与Spring Boot技术的信息化管理系统,实现古树资源的动态监测、数据管理与科学保护,推动生态、文化与经济可持续发展。
2025基于springboot的校车预定全流程管理系统
针对传统校车管理效率低、信息不透明等问题,本研究设计并实现了一套校车预定全流程管理系统。系统采用Spring Boot、Java、Vue和MySQL等技术,实现校车信息管理、在线预定、实时监控等功能,提升学校管理效率,保障学生出行安全,推动教育信息化发展。
基于springboot的大学公文收发管理系统
本文介绍公文收发系统的研究背景与意义,分析其在数字化阅读趋势下的必要性。系统采用Vue、Java、Spring Boot与MySQL技术,实现高效、便捷的公文管理与在线阅读,提升用户体验与信息处理效率。
|
人工智能 Java 关系型数据库
基于springboot的画品交流系统
本项目构建基于Java+Vue+SpringBoot+MySQL的画品交流系统,旨在解决传统艺术交易信息不透明、流通受限等问题,融合区块链与AI技术,实现画品展示、交易、鉴赏与社交一体化,推动艺术数字化转型与文化传播。
|
JavaScript Java 关系型数据库
基于springboot的高校运动会系统
本系统基于Spring Boot、Vue与MySQL,实现高校运动会报名、赛程安排及成绩管理的全流程信息化,提升组织效率,杜绝信息错漏与冒名顶替,推动体育赛事智能化发展。
基于springboot的大学生兼职系统
本课题针对大学生兼职信息不对称、权益难保障等问题,研究基于Spring Boot、Vue、MySQL等技术的兼职系统,旨在构建安全、高效、功能完善的平台,提升大学生就业竞争力与兼职质量。
基于springboot的养老院管理系统
随着人口老龄化加剧,传统养老院管理效率低下,亟需信息化升级。本文基于Java、Spring Boot、Vue等技术构建智慧养老系统,结合MySQL与MyBatis实现数据高效管理,提升服务精准性与运营效率,推动养老服务向智能化、现代化发展。