• Reggie外卖项目 —— 分类管理模块之删除分类功能


    13、删除分类

    13.1、需求分析

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

    在这里插入图片描述

    13.2、代码开发

    13.2.1、页面逻辑

    1、页面逻辑

    在这里插入图片描述

    2、页面请求

    在这里插入图片描述

    13.2.2、执行过程

    在开发代码之前,需要梳理一下整个程序的执行过程:

    1、页面发送ajax请求,将参数(id)提交到服务端

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

    3、Service调用Mapper操作数据库

    13.3、功能测试

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        log.info("删除分类,id为:{}", id);
    
        categoryService.removeById(id);
        return R.success("分类信息删除成功");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    13.4、功能完善

    13.4.1、问题分析

    1、前面我们已经实现了根据id删除分类的功能,但是并没有检查删除的分类是否关联了菜品或者套餐,所以我们需要进行功能完善。

    2、要完善分类删除功能,需要先准备基础的类和接口:

    1)实体类DishSetmeal

    Dish.java

    /**
     菜品
     */
    @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;
    
        //是否删除
        private Integer isDeleted;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    Setmeal.java

    /**
     * 套餐
     */
    @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;
    
        //是否删除
        private Integer isDeleted;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    2)Mapper接口DishMapperSetmealMapper

    DishMapper.java

    @Mapper
    public interface DishMapper extends BaseMapper<Dish> {
    }
    
    • 1
    • 2
    • 3

    SetmealMapper.java

    @Mapper
    public interface SetmealMapper extends BaseMapper<Setmeal> {
    }
    
    • 1
    • 2
    • 3

    3)Service接口DishServiceSetmealService

    DishService.java

    public interface DishService extends IService<Dish> {
    }
    
    • 1
    • 2

    SetmealService.java

    public interface SetmealService extends IService<Setmeal> {
    }
    
    • 1
    • 2

    4)Service实现类DishServiceImplSetmealServiceImpl

    DishServiceImpl.java

    @Service
    @Slf4j
    public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    }
    
    • 1
    • 2
    • 3
    • 4

    SetmealServiceImpl.java

    @Service
    @Slf4j
    public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    }
    
    • 1
    • 2
    • 3
    • 4

    13.4.2、代码开发

    1、自定义业务异常类

    CustomException.java

    /**
     * 自定义业务异常类
     */
    public class CustomException extends RuntimeException{
    
        public CustomException(String message) {
            super(message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、在全局异常处理器中处理自定义异常

    GlobalExceptionHandler.java

    /**
     * 处理自定义类异常
     * @param ex
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException ex) {
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、自定义删除功能

    CategoryServiceImpl.java

    @Service
    public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    
        @Autowired
        private DishService dishService;
        @Autowired
        private SetmealService setmealService;
    
        /**
         * 根据id删除分类,删除之前需要进行判断
         * @param id
         */
        @Override
        public void remove(Long id) {
            LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加查询条件,根据分类id进行查询
            dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
            int count1 = dishService.count(dishLambdaQueryWrapper);
            //查询当前分类是否关联了菜品,如果已经关联,抛出一个业务异常
            if (count1 > 0) {
                //已经关联菜品,抛出一个业务异常
                throw new CustomException("当前分类下关联了菜品,不能删除");
            }
    
            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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    4、修改controller

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        log.info("删除分类,id为:{}", id);
    
        //categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

  • 相关阅读:
    暑期JAVA学习(43.2)反射
    关于HSV了解这些就够了,python-opencv获取图片精确hsv的值
    免杀Bdfproxy
    Hadoop 之文件读取
    Git 开源的版本控制系统入门使用介绍 git 对比 svn
    Linux- 僵尸进程(Zombie Process)
    精通Nginx(17)-安全管控
    【web渗透思路】框架敏感信息泄露(特点、目录、配置)
    最新字节大佬总结出这份Java面试技术栈手册,建议收藏
    CSS基础
  • 原文地址:https://blog.csdn.net/kuaixiao0217/article/details/126860861