从明天起,做一个幸福的人
喂马、劈柴,周游世界
从明天起,关心粮食和蔬菜
我有一所房子,面朝大海,春暖花开
从明天起,和每一个亲人通信
告诉他们我的幸福
那幸福的闪电告诉我的
我将告诉每一个人
给每一条河每一座山取一个温暖的名字
陌生人,我也为你祝福
愿你有一个灿烂的前程
愿你有情人终成眷属
愿你在尘世获得幸福
我只愿面朝大海,春暖花开
新建 src/main/java/com/reggie/entity/Category
类:
package com.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 lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 分类
*/
@Data
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//类型 1 菜品分类 2 套餐分类
private Integer type;
//分类名称
private String name;
//顺序
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;
}
新建 src/main/java/com/reggie/mapper/CategoryMapper
接口:
package com.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reggie.entity.Category;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}
新建 src/main/java/com/reggie/service/CategoryService
类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Category;
public interface CategoryService extends IService<Category> {
}
新建 src/main/java/com/reggie/service/impl/CategoryServiceImpl
类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.Category;
import com.reggie.service.CategoryService;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
}
新建 src/main/java/com/reggie/controller/CategoryController
类:
package com.reggie.controller;
/**
* 分类管理
*/
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
}
编辑 src/main/java/com/reggie/controller/CategoryController
类:
...
/**
* 分类管理
*/
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* 新增分类
* @param category
* @return
*/
@PostMapping
public R<String> save(@RequestBody Category category) {
log.info("新增分类:{}", category);
categoryService.save(category);
return R.success("新增分类成功");
}
}
编辑 src/main/java/com/reggie/controller/CategoryController
类:
...
@GetMapping("/page")
public R<Page> page(int page, int pageSize) {
// 分页构造器
Page<Category> pageInfo = new Page<>(page, pageSize);
// 条件构造器
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
// 根据 sort 进行排序
queryWrapper.orderByAsc(Category::getSort);
// 分页查询
categoryService.page(pageInfo, queryWrapper);
return R.success(pageInfo);
}
编辑 src/main/java/com/reggie/controller/CategoryController
类:
...
/**
* 根据 id 删除分类
* @param id
* @return
*/
@DeleteMapping
public R<String> delete(Long id) {
log.info("删除分类,id 为:{}", id);
// categoryService.removeById(id);
// 增加了判断分类下 套餐、菜品的是否存在关联
categoryService.remove(id);
return R.success("删除分类成功");
}
删除分类,需要检查该分类下是否关联了菜品和套餐
新建 src/main/java/com/reggie/entity/Dish
类:
package com.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;
}
新建 src/main/java/com/reggie/entity/Setmeal
类:
package com.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;
}
新建 src/main/java/com/reggie/mapper/DishMapper
接口:
package com.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reggie.entity.Dish;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
}
新建 src/main/java/com/reggie/mapper/SetmealMapper
接口:
package com.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reggie.entity.Setmeal;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SetmealMapper extends BaseMapper<Setmeal> {
}
新建 src/main/java/com/reggie/service/DishService
类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Dish;
public interface DishService extends IService<Dish> {
}
新建 src/main/java/com/reggie/service/CategoryService
类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Setmeal;
public interface SetmealService extends IService<Setmeal> {
}
新建 src/main/java/com/reggie/service/impl/DishServiceImpl
类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.Dish;
import com.reggie.mapper.DishMapper;
import com.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
}
新建 src/main/java/com/reggie/service/impl/SetmealServiceImpl
类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.Setmeal;
import com.reggie.mapper.SetmealMapper;
import com.reggie.service.SetmealService;
import org.springframework.stereotype.Service;
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
}
新建 src/main/java/com/reggie/common/CustomException
类:
package com.reggie.common;
/**
* 自定义业务异常类
*/
public class CustomException extends RuntimeException{
public CustomException(String message) {
super(message);
}
}
编辑 src/main/java/com/reggie/service/CategoryService
类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Category;
public interface CategoryService extends IService<Category> {
public void remove(Long id);
}
编辑 src/main/java/com/reggie/service/impl/CategoryServiceImpl
类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.CustomException;
import com.reggie.entity.Category;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.mapper.CategoryMapper;
import com.reggie.service.CategoryService;
import com.reggie.service.DishService;
import com.reggie.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<>();
// 添加条件查询,根据 id 进行查询
dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
int count1 = dishService.count(dishLambdaQueryWrapper);
// 查询当前分类是否关联了菜品,
if(count1 > 0) {
// 如果有关联,则抛出一个异常
throw new CustomException("当前分类下关联了菜品,不能删除!");
}
// 套餐 校验
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
int count2 = setmealService.count(setmealLambdaQueryWrapper);
// 查询当前分类是否关联了套餐
if(count2 > 0 ) {
// 如果有关联,则抛出一个异常
throw new CustomException("当前分类下关联了套餐,不能删除!");
}
// 正常删除
super.removeById(id);
}
}
新建 src/main/java/com/reggie/common/GlobalExceptionHandler
类:
...
/**
* 异常方法处理
* @param ex
* @return
*/
@ExceptionHandler(CustomException.class)
public R<String> exceptionHandler(CustomException ex) {
log.info(ex.getMessage());
// 提示该分类下存在 菜品/套餐
return R.error(ex.getMessage());
}
}
编辑 src/main/java/com/reggie/controller/CategoryController
类:
...
/**
* 根据 id 修改分类信息
* @param category
* @return
*/
@PutMapping
public R<String> update(@RequestBody Category category) {
log.info("修改分类信息:{}", category);
categoryService.updateById(category);
return R.success("修改分类信息成功 ");
}
}