实现思路:
1. 在需要删除的表的service中自定义remove方法,在remove中实现对关联数据的查询,如果查询到关联的数据,则说明不能删除,否则可以删除。
2. 如果查询到有数据,即不能删除数据,则报出异常(自定义异常),并且返回给前端。
根据id删除分类的功能,是需要检查删除的分类是否关联了菜品或者套餐,所以我们需要进行功能完善。
要完善分类删除功能,需要先准备基础的类和接口:
package com.mannor.reggie_take_out.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;
//是否删除
private Integer isDeleted;
}
package com.mannor.reggie_take_out.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;
//是否删除
private Integer isDeleted;
}
package com.mannor.reggie_take_out.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mannor.reggie_take_out.entity.Dish;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
}
package com.mannor.reggie_take_out.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mannor.reggie_take_out.entity.Setmeal;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SetmealMapper extends BaseMapper<Setmeal> {
}
package com.mannor.reggie_take_out.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mannor.reggie_take_out.entity.Dish;
public interface DishService extends IService<Dish> {
}
package com.mannor.reggie_take_out.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mannor.reggie_take_out.entity.Setmeal;
public interface SetmealService extends IService<Setmeal> {
}
package com.mannor.reggie_take_out.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mannor.reggie_take_out.entity.Dish;
import com.mannor.reggie_take_out.mapper.DishMapper;
import com.mannor.reggie_take_out.service.DishService;
import org.springframework.stereotype.Service;
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
}
package com.mannor.reggie_take_out.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mannor.reggie_take_out.entity.Setmeal;
import com.mannor.reggie_take_out.mapper.SetmealMapper;
import com.mannor.reggie_take_out.service.SetmealService;
import org.springframework.stereotype.Service;
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
}
package com.mannor.reggie_take_out.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mannor.reggie_take_out.common.CustomException;
import com.mannor.reggie_take_out.entity.Category;
import com.mannor.reggie_take_out.entity.Dish;
import com.mannor.reggie_take_out.entity.Setmeal;
import com.mannor.reggie_take_out.mapper.CategoryMapper;
import com.mannor.reggie_take_out.service.CategoryService;
import com.mannor.reggie_take_out.service.DishService;
import com.mannor.reggie_take_out.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@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<>();
dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);//添加查询条件--》id
int count1 = dishService.count(dishLambdaQueryWrapper);
//查询当前分类是否关联了菜品,如果已经关联,抛出一个业务异常
if (count1 > 0) {
//关联了菜品,抛出业务异常
throw new CustomException("当前分类下关联了菜品,不能删除");
}
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);//添加查询条件--》id
int count2 = setmealService.count(setmealLambdaQueryWrapper);
//查询当前分类是否关联了套餐,如果已经关联,抛出一个业务异常
if (count1 > 0) {
//关联了套餐,抛出业务异常
throw new CustomException("当前分类下关联了套餐,不能删除");
}
//正常删除分类
super.removeById(id);
}
}
package com.mannor.reggie_take_out.common;
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}
package com.mannor.reggie_take_out.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLIntegrityConstraintViolationException;
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
/**
* 异常处理方法(分类删除的自定义异常处理)
* @param ex
* @return
*/
@ExceptionHandler(CustomException.class)
public R<String> exceptionHandler(CustomException ex) {
log.error(ex.getMessage());
return R.error(ex.getMessage());
}
}