• 瑞吉外卖 —— 4、菜品分类管理


    目录

    1、公共字段自动填充 

    1.1、分析

    1.2、代码

    1.2.1、设置自动填充字段

    1.2.2、BaseContext 工具类

    1.2.3、在登录过滤器 LoginCheckFilter 中将登录的员工 id 存储到 BaseContext

    1.2.4、元数据对象处理器

    2、新增分类

    2.1、分析

    2.1.1、需求分析 

    2.1.2、数据模型

    2.1.3、执行流程

    2.2、代码

    3、分类信息分页查询

    3.1、分析

    3.1.1、需求分析

    3.1.2、执行流程

    3.2、代码

    4、删除分类

    4.1、分析

    4.1.1、需求分析

    4.1.2、执行流程

    4.2、代码

    4.3、功能完善

    4.3.1、自定义异常类 CustomException

    4.3.2、为 CustomException 异常设置处理方法

    4.3.3、在 CategoryServiceImpl 中添加根据 id 删除分类的方法

    4.3.4、修改 CategoryController 中的删除分类方法

    5、修改分类

    5.1、需求分析


    1、公共字段自动填充 

    1.1、分析

    1.2、代码

    1.2.1、设置自动填充字段

    在 Employee 类的对应字段加上 @TableField 注解,并设置 fill 属性值

    1. @TableField(fill = FieldFill.INSERT) // 插入时填充字段
    2. private LocalDateTime createTime;
    3. @TableField(fill = FieldFill.INSERT_UPDATE) // 插入和更新时填充字段
    4. private LocalDateTime updateTime;
    5. @TableField(fill = FieldFill.INSERT) // 插入时填充字段
    6. private Long createUser;
    7. @TableField(fill = FieldFill.INSERT_UPDATE) // 插入和更新时填充字段
    8. private Long updateUser;

    1.2.2、BaseContext 工具类

    为了动态的获取员工的 id,这里使用 threadLocal 这个局部变量来获取和存储员工 id 

    1. package com.itheima.reggie.common;
    2. /**
    3. * @Author zhang
    4. * @Date 2022/9/2 - 14:43
    5. * @Version 1.0
    6. */
    7. // 基于ThreadLocal封装工具类,用户保存和获取当前登录用户id
    8. public class BaseContext {
    9. private static ThreadLocal threadLocal = new ThreadLocal<>();
    10. public static void setCurrentId(Long id){
    11. threadLocal.set(id);
    12. }
    13. public static Long getCurrentId(){
    14. return threadLocal.get();
    15. }
    16. }

    1.2.3、在登录过滤器 LoginCheckFilter 中将登录的员工 id 存储到 BaseContext

     在登录检查到已登陆后将登录的员工 id 存储到自定义的 BaseContext 工具类

    1. // 已登陆,放行
    2. if(request.getSession().getAttribute("employee") != null){
    3. Long id = Thread.currentThread().getId();
    4. log.info("线程id为{}", id);
    5. //log.info("用户已登录,放行,用户id为{}", request.getSession().getAttribute("employee"));
    6. Long employeeId = (Long) request.getSession().getAttribute("employee");
    7. BaseContext.setCurrentId(employeeId);
    8. filterChain.doFilter(request, response);
    9. return;
    10. }

    1.2.4、元数据对象处理器

    1. package com.itheima.reggie.common;
    2. import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.apache.ibatis.reflection.MetaObject;
    5. import org.springframework.stereotype.Component;
    6. import java.time.LocalDateTime;
    7. /**
    8. * @Author zhang
    9. * @Date 2022/9/2 - 10:54
    10. * @Version 1.0
    11. */
    12. // 自定义元数据对象处理器
    13. @Component
    14. @Slf4j
    15. public class MyMetaObjecthandler implements MetaObjectHandler {
    16. /**
    17. * 插入操作自动填充
    18. * @param metaObject
    19. */
    20. @Override
    21. public void insertFill(MetaObject metaObject) {
    22. log.info("公共字段自动填充[insert]...");
    23. log.info(metaObject.toString());
    24. metaObject.setValue("createTime", LocalDateTime.now());
    25. metaObject.setValue("updateTime", LocalDateTime.now());
    26. metaObject.setValue("createUser", BaseContext.getCurrentId());
    27. metaObject.setValue("updateUser", BaseContext.getCurrentId());
    28. }
    29. /**
    30. * 插入操作自动填充
    31. * @param metaObject
    32. */
    33. @Override
    34. public void updateFill(MetaObject metaObject) {
    35. log.info("公共字段自动填充[update]...");
    36. log.info(metaObject.toString());
    37. Long id = Thread.currentThread().getId();
    38. log.info("线程id为{}", id);
    39. metaObject.setValue("updateTime", LocalDateTime.now());
    40. metaObject.setValue("updateUser", BaseContext.getCurrentId());
    41. }
    42. }

    2、新增分类

    2.1、分析

    2.1.1、需求分析 

    2.1.2、数据模型

    新增分类,其实就是将我们新增窗口录入的分类数据插入到 category 表,表结构如下:

    从资料去复制实体 Category 类到 entity 包,然后创建对应的 mapper、service及其实现类、controller

    2.1.3、执行流程

    2.2、代码

    1. package com.itheima.reggie.controller;
    2. import com.itheima.reggie.common.R;
    3. import com.itheima.reggie.entity.Category;
    4. import com.itheima.reggie.service.CategoryService;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.web.bind.annotation.PostMapping;
    8. import org.springframework.web.bind.annotation.RequestBody;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import org.springframework.web.bind.annotation.RestController;
    11. /**
    12. * @Author zhang
    13. * @Date 2022/9/2 - 15:13
    14. * @Version 1.0
    15. */
    16. @RestController
    17. @Slf4j
    18. @RequestMapping("/category")
    19. public class CategoryController {
    20. @Autowired
    21. private CategoryService categoryService;
    22. /**
    23. * 新增分类
    24. * @param category
    25. * @return
    26. */
    27. @PostMapping
    28. public R save(@RequestBody Category category){
    29. log.info("category:{}", category.toString());
    30. categoryService.save(category);
    31. return R.success("新增分类成功");
    32. }
    33. }

    3、分类信息分页查询

    3.1、分析

    3.1.1、需求分析

    3.1.2、执行流程

    3.2、代码

    1. /**
    2. * 分类信息分页查询
    3. * @param page
    4. * @param pageSize
    5. * @return
    6. */
    7. @GetMapping("/page")
    8. public R page(
    9. int page,
    10. int pageSize
    11. ){
    12. Page pageInfo = new Page<>(page, pageSize);
    13. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
    14. queryWrapper.orderByAsc(Category::getSort);
    15. categoryService.page(pageInfo);
    16. return R.success(pageInfo);
    17. }

    4、删除分类

    4.1、分析

    4.1.1、需求分析

    4.1.2、执行流程

    4.2、代码

    1. /**
    2. * 根据id删除对应的分类
    3. * @param ids
    4. * @return
    5. */
    6. @DeleteMapping
    7. public R delete(Long ids){
    8. log.info("删除id为{}的分类", ids);
    9. categoryService.removeById(ids);
    10. return R.success("分类信息删除成功");
    11. }

    4.3、功能完善

    4.3.1、自定义异常类 CustomException

    自定义异常类 CustomException 用于删除当前分类时,若该分类关联了菜品或套餐,则不能删除,抛出该异常。

    1. public class CustomException extends RuntimeException{
    2. public CustomException(String message){
    3. super(message);
    4. }
    5. }

    4.3.2、为 CustomException 异常设置处理方法

    在 GlobalExceptionHandle 全局异常处理类中添加捕获 CustomException 异常后的处理方法

    1. /**
    2. * 异常处理方法:删除分类异常
    3. * @return
    4. */
    5. @ExceptionHandler(CustomException.class)
    6. public R exceptionHandler(CustomException exception){
    7. log.error(exception.getMessage());
    8. return R.error(exception.getMessage());
    9. }

    4.3.3、在 CategoryServiceImpl 中添加根据 id 删除分类的方法

    1. package com.itheima.reggie.service.impl;
    2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    4. import com.itheima.reggie.common.CustomException;
    5. import com.itheima.reggie.entity.Category;
    6. import com.itheima.reggie.entity.Dish;
    7. import com.itheima.reggie.entity.Setmeal;
    8. import com.itheima.reggie.mapper.CategoryMapper;
    9. import com.itheima.reggie.service.CategoryService;
    10. import com.itheima.reggie.service.DishSerivce;
    11. import com.itheima.reggie.service.SetmealService;
    12. import org.springframework.beans.factory.annotation.Autowired;
    13. import org.springframework.stereotype.Service;
    14. /**
    15. * @Author zhang
    16. * @Date 2022/9/2 - 15:11
    17. * @Version 1.0
    18. */
    19. @Service
    20. public class CategoryServiceImpl extends ServiceImpl implements CategoryService {
    21. @Autowired
    22. private DishSerivce dishSerivce;
    23. @Autowired
    24. private SetmealService setmealService;
    25. /**
    26. * 根据id删除分类信息,删除之前需要判断是否关联菜品或套餐
    27. * @param id
    28. */
    29. @Override
    30. public void remove(Long id) {
    31. // 查询当前分类是否关联了菜品,如果已关联,抛出一个业务异常
    32. LambdaQueryWrapper dishQueryWrapper = new LambdaQueryWrapper();
    33. dishQueryWrapper.eq(Dish::getCategoryId, id);
    34. int dishCount = dishSerivce.count(dishQueryWrapper);
    35. if(dishCount > 0){
    36. throw new CustomException("当前分类下关联了菜品,删除失败");
    37. }
    38. // 查询当前分类是否关联了套餐,如果已关联,抛出一个业务异常
    39. LambdaQueryWrapper setmealQueryWrapper = new LambdaQueryWrapper();
    40. setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
    41. int setmealCount = setmealService.count(setmealQueryWrapper);
    42. if(setmealCount > 0){
    43. throw new CustomException("当前分类下关联了套餐,删除失败");
    44. }
    45. // 正常删除分类
    46. super.removeById(id);
    47. }
    48. }

    4.3.4、修改 CategoryController 中的删除分类方法

    1. /**
    2. * 根据id删除对应的分类
    3. * @param ids
    4. * @return
    5. */
    6. @DeleteMapping
    7. public R delete(Long ids){
    8. log.info("删除id为{}的分类", ids);
    9. //categoryService.removeById(ids);
    10. categoryService.remove(ids);
    11. return R.success("分类信息删除成功");
    12. }

    5、修改分类

    5.1、需求分析

    接口:

     

  • 相关阅读:
    使用Composition API和setup语法糖重构Vue组件
    损失函数——机器学习
    使用HTML制作静态网站:传统文化戏剧锡剧带psd设计图(2个页面)
    高效管理文件:如何通过文件数量归类提高工作效率
    Vue 中setup的特性
    外包干了3个月,技术退步明显。。。。。
    InnoDB 事务的四种隔离机制以及底层实现原理
    ZCMU--1720: 死亡如风,我要装逼
    SQL面试题练习 —— 查询每个产品每年总销售额
    mac IDEA激活 亲测有效
  • 原文地址:https://blog.csdn.net/Mr_zhangyj/article/details/126655772