需求分析:
在分类管理列表页面,可以对某个分类进行删除操作。需要注意的是当前分类关注了菜品或者套餐时,此分类不允许删除
代码开发
1、页面发送ajax请求,将参数(id)提交到服务端
2、服务端Controller接收页面提交的数据并调用Service删除数据
3、Service调用Mapper操作数据库
/** * 根据id删除菜品分类 * @param id * @return */ @DeleteMapping() public R<String> delete(@RequestParam("ids") Long id){ log.info("id:{}", id); categoryService.removeById(id); return R.success("分类信息删除成功"); }
功能完善:检查要删除的分类是否在具体菜品中存在,若存在则不能删除。
准备工作:
1、实体类Dish和Setmeal
2、Mapper接口DishMapper和SetmealMapper
3、Service接口DishService和SetmealService
4、Service实现类DishServiceImpl和SetmealServiceImpl
实现:
(1)由于删除前要进行category_id是否关联其它菜品校验,因此在CategoryServiceImpl中自定义一个remove()方法。
package com.itheima.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.itheima.common.CustomException; import com.itheima.common.R; import com.itheima.entity.Category; import com.itheima.entity.Dish; import com.itheima.entity.Setmeal; import com.itheima.mapper.CategoryMapper; import com.itheima.service.CategoryService; import com.itheima.service.DishService; import com.itheima.service.SetmealService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service @Slf4j 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<Dish>(); dishLambdaQueryWrapper.eq(Dish::getCategoryId, id); int count1 = dishService.count(dishLambdaQueryWrapper); if(count1 > 0){ //已经关联菜品,抛出一个业务异常 throw new CustomException("当前分类下关联了菜品,不能删除"); } //查询该类别是否关联具体套餐,如果已经关联,抛出一个业务异常 LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<Setmeal>(); setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id); int count2 = setmealService.count(setmealLambdaQueryWrapper); if(count2 > 0){ //已经关联套餐,抛出一个业务异常 throw new CustomException("当前分类下关联了套餐,不能删除"); } //正常删除分类 super.removeById(id); } }
(2)倘若要删除的category_id与某个菜品或者套餐具有关联性,则抛出一个自定义异常CustomException
package com.itheima.common; /** * 自定义业务异常 */ public class CustomException extends RuntimeException{ public CustomException(String message){ super(message); } }
(3)在全局异常处理中捕获该异常并返回结果
package com.itheima.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; /** * 全局异常处理 */ //拦截类上加了注解@RestController的Controller、拦截一般controller @ControllerAdvice(annotations = {RestController.class, Controller.class}) @ResponseBody @Slf4j public class GlobalExceptionHandler { /** * 异常处理方法 * @return */ @ExceptionHandler(SQLIntegrityConstraintViolationException.class) public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){ //输出异常信息 log.error(ex.getMessage()); if(ex.getMessage().contains("Duplicate entry")){ String[] split = ex.getMessage().split(" "); String msg = split[2] + "已存在"; return R.error(msg); } return R.error("未知错误"); } /** * 捕获自定义异常 * @param ex * @return */ @ExceptionHandler(CustomException.class) public R<String> exceptionHandler(CustomException ex){ //输出异常信息 log.error(ex.getMessage()); return R.error(ex.getMessage()); } }
(4)在CategoryController中调用remove()方法来删除菜品分类
/** * 根据id删除菜品分类 * @param id * @return */ @DeleteMapping() public R<String> delete(@RequestParam("ids") Long id){ log.info("id:{}", id); //categoryService.removeById(id); categoryService.remove(id); return R.success("分类信息删除成功"); }
标签:删除,分类,菜品,import,com,id,itheima From: https://www.cnblogs.com/fxzm/p/17184537.html