完善删除操作(项目必备)

建包建类看这

第一步:看到最下面的删除操作

/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);

        return R.success("新增添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        log.info("page = {},pageSize = {}",page,pageSize);

        //构造分页构造器
        Page<Category> pageInfo = new Page(page, pageSize);

        //排序
        LambdaQueryWrapper<Category> queryWrapper = Wrappers.lambdaQuery(Category.class)
                .orderByAsc(Category::getSort);

        //执行查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 修改操作
     * @param category
     * @return
     */
    @PutMapping
    public R<String> put(@RequestBody Category category){
        categoryService.updateById(category);

        return R.success("修改成功");
    }

    /**
     * 删除操作
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        categoryService.removeById(ids);

        return R.success("删除成功");
    }
}

第二步:重写removeById()方法

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类,删除之前需要进行判断
     *
     * @param id 主键ID
     * @return
     */
    @Override
    public boolean removeById(Serializable id) {

        //select count(*) from dish where category_id = id;
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = Wrappers.lambdaQuery(Dish.class)
                .eq(Dish::getCategoryId, id);
        int count = dishService.count(dishLambdaQueryWrapper);

        //有菜品,抛出个业务异常
        if(count > 0){
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }

        //select count(*) from setmeal where category_id = id;
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                .eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);

        //有套餐,抛出个业务异常
        if(count > 0){
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }

        return super.removeById(id);
    }
}

第三步:自定义异常类

 

/**
 * 自定义业务异常类
 */
public class CustomException extends RuntimeException{

    public CustomException(String message){
        super(message);
    }
}

第四步:将自定义类添加到全局异常类中

最下面那个异常处理方法就是自己自定义的那个自定义异常类

/**
 * 全局异常处理
 */
@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());

        //contains() 方法用于判断字符串中是否包含指定的字符或字符串。
        if(ex.getMessage().contains("Duplicate entry")){
            //split(" ")去除空格
            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());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值