瑞吉外卖实战学习--13、完善删除中的逻辑

前言

本项目gitee位置:添加链接描述
之前实现了直接删除分类功能,并没有实现分类删除中的逻辑编写,本篇文章主要是针对删除逻辑来进行讲解,
本篇文章需要使用到Dish(菜品)和Setmeal(套餐)表,需要根据mybatisPlus规范创建实体类和接口类

效果图

在这里插入图片描述

逻辑介绍

检测分类删除的时候是否关联了别的菜品或者套餐

表结构

在这里插入图片描述
在这里插入图片描述

根据mybatisPlus接口规范创建实体类和service和mapper文件

1、实体类Dish和Setmeal

在这里插入图片描述

2、Mapper接口DishMapper和SetealMapper

在这里插入图片描述

3、Service接口DishService和setmealService

在这里插入图片描述

4、Service实现类DishServiceImpl和setmealServicelmpl

切记加@Service注解否则报错
在这里插入图片描述

编写删除函数

在这里插入图片描述

1、将创建刚才两个表的实体类

在这里插入图片描述

2、获取表中categoryId和id相等的数据

3、判断获取到的数据有几条

4、如果大于0说明绑定了数据就抛出异常

5、如果都不大于0就通过super调用通过id删除的方法

package com.example.ruiji_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ruiji_demo.common.CustomException;
import com.example.ruiji_demo.entity.Category;
import com.example.ruiji_demo.entity.Dish;
import com.example.ruiji_demo.entity.Setmeal;
import com.example.ruiji_demo.mapper.CategoryMapper;
import com.example.ruiji_demo.service.CategoryService;
import com.example.ruiji_demo.service.DishService;
import com.example.ruiji_demo.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jitwxs
 * @date 2024年04月01日 21:44
 */
@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<>();
//        获取表中categoryId和id相等的数据
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
//        判断获取到的数据有几条
        Long count1 = dishService.count(dishLambdaQueryWrapper);

//        如果大于0说明关联了菜品
        if(count1>0){
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }

//        创建套餐的实例
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        Long count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2>0){
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }

        super.removeById(id);
    }

}

自定义业务异常类

在这里插入图片描述

package com.example.ruiji_demo.common;

/**
 * 自定义业务异常类
 * @author jitwxs
 * @date 2024年04月03日 16:52
 */
public class CustomException extends RuntimeException {
    public CustomException(String message){
        super(message);
    }
}

在全局异常处理器中捕获异常

在这里插入图片描述

package com.example.ruiji_demo.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;

/**
 * @author jitwxs
 * @date 2024年03月30日 22:24
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 异常处理方法
     * @param ex
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
//        获取到是否是含有Duplicate entry的错误,如果是就提取出来名称,然后添加到报错信息
        if(ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return R.error(msg);
        }
        return R.error("添加失败");
    }

    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException ex){
        return R.error(ex.getMessage());
    }
}

  • 28
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值