需求分析
代码开发
前端测试
添加CategoryController.java方法
这里必须要用ids参数,不然无法捕获id,原因未知
/**
* 根据id删除分类
* @param id
* @return
*/
@DeleteMapping
public R<String> delete(Long ids){
log.info("删除分类,id为:{}",ids);
categoryService.removeById(ids);
return R.success("分类信息删除成功");
}
功能测试
正常
功能完善
模块搭建
实体类Dish.java
package com.taotao.reggie.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;
}
DishMapper.java
package com.taotao.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.reggie.entity.Dish;
import org.apache.ibatis.annotations.Mapper;
/**
* create by 刘鸿涛
* 2022/6/8 8:16
*/
@SuppressWarnings({"all"})
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
}
DisService.java
package com.taotao.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.reggie.entity.Dish;
/**
* create by 刘鸿涛
* 2022/6/8 8:18
*/
@SuppressWarnings({"all"})
public interface DishService extends IService<Dish> {
}
DishServiceImpl.java
package com.taotao.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.entity.Dish;
import com.taotao.reggie.mapper.DishMapper;
import com.taotao.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
/**
* create by 刘鸿涛
* 2022/6/8 8:21
*/
@SuppressWarnings({"all"})
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
}
实体类Setmeal.java
package com.taotao.reggie.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;
}
SetmealMapper.java
package com.taotao.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.reggie.entity.Setmeal;
import org.apache.ibatis.annotations.Mapper;
/**
* create by 刘鸿涛
* 2022/6/8 8:17
*/
@SuppressWarnings({"all"})
@Mapper
public interface SetmealMapper extends BaseMapper<Setmeal> {
}
SetmealService.java
package com.taotao.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.reggie.entity.Setmeal;
/**
* create by 刘鸿涛
* 2022/6/8 8:20
*/
@SuppressWarnings({"all"})
public interface SetmealService extends IService<Setmeal> {
}
SetmealServiceImpl.java
package com.taotao.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.entity.Setmeal;
import com.taotao.reggie.mapper.SetmealMapper;
import com.taotao.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
/**
* create by 刘鸿涛
* 2022/6/8 8:23
*/
@SuppressWarnings({"all"})
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
}
功能实现
CategoryService.java添加方法
void remove(Long id);
方法改造
既然是改造mybatis框架方法,那么就需要在service层写业务
编写CategoryServiceImpl.java
package com.taotao.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.common.CustomException;
import com.taotao.reggie.entity.Category;
import com.taotao.reggie.entity.Dish;
import com.taotao.reggie.entity.Setmeal;
import com.taotao.reggie.mapper.CategoryMapper;
import com.taotao.reggie.service.CategoryService;
import com.taotao.reggie.service.DishService;
import com.taotao.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* create by 刘鸿涛
* 2022/6/7 7:12
*/
@SuppressWarnings({"all"})
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private DishService dishService;
@Autowired
private SetmealService setmealService;
/**
* 根据id删除分类,删除之前需要进行判断
* @param id
*/
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();
if(count2 > 0){
throw new CustomException("当前分类下关联了套餐,不能删除");
//已经关联了套餐,抛出一个业务异常
}
//正常删除分类
super.removeById(id);
}
}
自定义业务异常CustomException.java
package com.taotao.reggie.common;
/**
* create by 刘鸿涛
* 2022/6/8 9:03
*/
@SuppressWarnings({"all"})
public class CustomException extends RuntimeException{
public CustomException(String message){
super(message);
}
}
GlobalExceptionHandler.java添加方法
/**
* 异常处理
* @param ex
* @return
*/
@ExceptionHandler(CustomException.class)
public R<String> exceptionHandler(CustomException ex){
log.error(ex.getMessage());
return R.error(ex.getMessage());
}
功能测试
有关联失败
无关联成功
添加测试分类