目录
1.2.3、在登录过滤器 LoginCheckFilter 中将登录的员工 id 存储到 BaseContext
4.3.2、为 CustomException 异常设置处理方法
4.3.3、在 CategoryServiceImpl 中添加根据 id 删除分类的方法
4.3.4、修改 CategoryController 中的删除分类方法






在 Employee 类的对应字段加上 @TableField 注解,并设置 fill 属性值
- @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;
为了动态的获取员工的 id,这里使用 threadLocal 这个局部变量来获取和存储员工 id
- package com.itheima.reggie.common;
-
- /**
- * @Author zhang
- * @Date 2022/9/2 - 14:43
- * @Version 1.0
- */
- // 基于ThreadLocal封装工具类,用户保存和获取当前登录用户id
- public class BaseContext {
-
- private static ThreadLocal
threadLocal = new ThreadLocal<>(); -
- public static void setCurrentId(Long id){
- threadLocal.set(id);
- }
-
- public static Long getCurrentId(){
- return threadLocal.get();
- }
-
- }
在登录检查到已登陆后将登录的员工 id 存储到自定义的 BaseContext 工具类
- // 已登陆,放行
- if(request.getSession().getAttribute("employee") != null){
- Long id = Thread.currentThread().getId();
- log.info("线程id为{}", id);
- //log.info("用户已登录,放行,用户id为{}", request.getSession().getAttribute("employee"));
- Long employeeId = (Long) request.getSession().getAttribute("employee");
- BaseContext.setCurrentId(employeeId);
-
- filterChain.doFilter(request, response);
- return;
- }
- package com.itheima.reggie.common;
-
- import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.ibatis.reflection.MetaObject;
- import org.springframework.stereotype.Component;
-
- import java.time.LocalDateTime;
-
- /**
- * @Author zhang
- * @Date 2022/9/2 - 10:54
- * @Version 1.0
- */
- // 自定义元数据对象处理器
- @Component
- @Slf4j
- public class MyMetaObjecthandler implements MetaObjectHandler {
-
- /**
- * 插入操作自动填充
- * @param metaObject
- */
- @Override
- public void insertFill(MetaObject metaObject) {
- log.info("公共字段自动填充[insert]...");
- log.info(metaObject.toString());
- metaObject.setValue("createTime", LocalDateTime.now());
- metaObject.setValue("updateTime", LocalDateTime.now());
- metaObject.setValue("createUser", BaseContext.getCurrentId());
- metaObject.setValue("updateUser", BaseContext.getCurrentId());
- }
-
- /**
- * 插入操作自动填充
- * @param metaObject
- */
- @Override
- public void updateFill(MetaObject metaObject) {
- log.info("公共字段自动填充[update]...");
- log.info(metaObject.toString());
- Long id = Thread.currentThread().getId();
- log.info("线程id为{}", id);
- metaObject.setValue("updateTime", LocalDateTime.now());
- metaObject.setValue("updateUser", BaseContext.getCurrentId());
- }
-
- }

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

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

- package com.itheima.reggie.controller;
-
- import com.itheima.reggie.common.R;
- import com.itheima.reggie.entity.Category;
- import com.itheima.reggie.service.CategoryService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @Author zhang
- * @Date 2022/9/2 - 15:13
- * @Version 1.0
- */
- @RestController
- @Slf4j
- @RequestMapping("/category")
- public class CategoryController {
-
- @Autowired
- private CategoryService categoryService;
-
- /**
- * 新增分类
- * @param category
- * @return
- */
- @PostMapping
- public R
save(@RequestBody Category category){ - log.info("category:{}", category.toString());
- categoryService.save(category);
- return R.success("新增分类成功");
- }
-
- }


- /**
- * 分类信息分页查询
- * @param page
- * @param pageSize
- * @return
- */
- @GetMapping("/page")
- public R
page( - int page,
- int pageSize
- ){
- Page
pageInfo = new Page<>(page, pageSize); - LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.orderByAsc(Category::getSort);
- categoryService.page(pageInfo);
- return R.success(pageInfo);
- }


- /**
- * 根据id删除对应的分类
- * @param ids
- * @return
- */
- @DeleteMapping
- public R
delete(Long ids){ - log.info("删除id为{}的分类", ids);
- categoryService.removeById(ids);
- return R.success("分类信息删除成功");
- }

自定义异常类 CustomException 用于删除当前分类时,若该分类关联了菜品或套餐,则不能删除,抛出该异常。
- public class CustomException extends RuntimeException{
-
- public CustomException(String message){
- super(message);
- }
-
- }
在 GlobalExceptionHandle 全局异常处理类中添加捕获 CustomException 异常后的处理方法
- /**
- * 异常处理方法:删除分类异常
- * @return
- */
- @ExceptionHandler(CustomException.class)
- public R
exceptionHandler(CustomException exception){ - log.error(exception.getMessage());
- return R.error(exception.getMessage());
- }
- package com.itheima.reggie.service.impl;
-
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.itheima.reggie.common.CustomException;
- import com.itheima.reggie.entity.Category;
- import com.itheima.reggie.entity.Dish;
- import com.itheima.reggie.entity.Setmeal;
- import com.itheima.reggie.mapper.CategoryMapper;
- import com.itheima.reggie.service.CategoryService;
- import com.itheima.reggie.service.DishSerivce;
- import com.itheima.reggie.service.SetmealService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @Author zhang
- * @Date 2022/9/2 - 15:11
- * @Version 1.0
- */
- @Service
- public class CategoryServiceImpl extends ServiceImpl
implements CategoryService { -
- @Autowired
- private DishSerivce dishSerivce;
-
- @Autowired
- private SetmealService setmealService;
-
- /**
- * 根据id删除分类信息,删除之前需要判断是否关联菜品或套餐
- * @param id
- */
- @Override
- public void remove(Long id) {
- // 查询当前分类是否关联了菜品,如果已关联,抛出一个业务异常
- LambdaQueryWrapper
dishQueryWrapper = new LambdaQueryWrapper(); - dishQueryWrapper.eq(Dish::getCategoryId, id);
- int dishCount = dishSerivce.count(dishQueryWrapper);
- if(dishCount > 0){
- throw new CustomException("当前分类下关联了菜品,删除失败");
- }
-
- // 查询当前分类是否关联了套餐,如果已关联,抛出一个业务异常
- LambdaQueryWrapper
setmealQueryWrapper = new LambdaQueryWrapper(); - setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
- int setmealCount = setmealService.count(setmealQueryWrapper);
- if(setmealCount > 0){
- throw new CustomException("当前分类下关联了套餐,删除失败");
- }
-
- // 正常删除分类
- super.removeById(id);
- }
-
- }
- /**
- * 根据id删除对应的分类
- * @param ids
- * @return
- */
- @DeleteMapping
- public R
delete(Long ids){ - log.info("删除id为{}的分类", ids);
- //categoryService.removeById(ids);
- categoryService.remove(ids);
- return R.success("分类信息删除成功");
- }

接口:
