瑞吉外卖03

1.公共字段自动填充

实体类中需要被统一管理的字段上上面加上 @TableField注解,并在内部添加fill属性,INSERT为在执行新增操作时执行,@TableField(fill = FieldFill.INSERT加在create time字段上,即在调用maybatis-plus的save方法时,便会自动为create time字段赋值,即达到了自动填充的效果,同理,加上UPDATE即在调用update方法时也会执行自动填充,此步骤完成后,并不能成功执行,我们还需要进行下一步配置

在common包创建类

 1 /**
 2  * 基于threadlocal封装工具类,用户保存和获取当前登录用户id
 3  */
 4 public class BaseContext {
 5     private static  ThreadLocal<Long> threadLocal=new ThreadLocal<>();
 6     public static void  setCurrentId(Long id){
 7         threadLocal.set(id);
 8     }
 9     public static Long   getCurrentId(){
10         return threadLocal.get();
11     }
12 
13 }
 1 package com.example.reggie.common;
 2 
 3 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 4 import lombok.extern.slf4j.Slf4j;
 5 import org.apache.ibatis.reflection.MetaObject;
 6 import org.springframework.stereotype.Component;
 7 
 8 import java.time.LocalDateTime;
 9 
10 /**
11  * 自定义元数据处理器
12  */
13 @Slf4j
14 @Component
15 public class MymetaObject implements MetaObjectHandler {
16     //插入自动填充
17     @Override
18     public void insertFill(MetaObject metaObject) {
19       metaObject.setValue("createTime", LocalDateTime.now());
20       metaObject.setValue("updateTime",LocalDateTime.now());
21       metaObject.setValue("createUser", BaseContext.getCurrentId());
22       metaObject.setValue("updateUser",BaseContext.getCurrentId());
23     }
24     //更新自动填充
25     @Override
26     public void updateFill(MetaObject metaObject) {
27         long id =Thread.currentThread().getId();
28         log.info("id:{}",id);
29         metaObject.setValue("updateTime",LocalDateTime.now());
30         metaObject.setValue("updateUser",BaseContext.getCurrentId());
31     }
32 }
BaseContext.getCurrentId()这里的id需要在LoginCheckFilter中设置

 

 2.新增分类

导入category实体类,完成mapper,service接口和接口实现类

在categorycontroller类中

 1 @RestController
 2 @RequestMapping("/category")
 3 public class CategoryController {
 4     @Autowired
 5     private CategoryService categoryService;
 6     @PostMapping
 7     public R<String> save(@RequestBody Category category){
 8         categoryService.save(category);
 9         return R.success("新增分类成功");
10     }

3.分类信息查询

 1 @GetMapping("/page")
 2     public  R<Page> page(int page,int pageSize){
 3         //分页构造器
 4         Page<Category> pageInfo= new Page<>(page,pageSize);
 5         //构造条件构造器
 6         LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper();
 7         queryWrapper.orderByAsc(Category::getSort);
 8         //进行分页查询
 9         categoryService.page(pageInfo,queryWrapper);
10         return R.success(pageInfo);
11     }

4.分类信息删除

 1  @DeleteMapping
 2 2     public R<String> delete(Long id){
 3 3         categoryService.remove(id);
 4 4         return R.success("分类信息删除成功");
 5 5     }
 6 //这里的remove方法是自定义的,因为删除的时候需要判断是否和其他表中的id有关联,所有先导入dish实体类,和setmeal实体类并完成相应的mapper和service接口和serviece的实现类
 7 //然后在CategoryServiceIml中完成自定义的remove接口
 8 /**
 9  *根据id删除分类,删之前判断是否有其他关联
10  */
11 @Autowired
12 private DishService dishService;
13 @Autowired
14 private SetMealService setMealService;
15 public void  remove(Long id){
16     LambdaQueryWrapper<Dish> dishLambdaQueryWrapper =new LambdaQueryWrapper<>();
17     dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
18     int count1= dishService.count(dishLambdaQueryWrapper);
19 
20     //查询当前分类是否关联了菜品,关联就抛出异常
21     if(count1>0){
22       throw new  CustomException("当前分类关联了菜品,无法删除");
23     }
24 
25     LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper =new LambdaQueryWrapper<>();
26     setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
27     int count2 =setMealService.count(setmealLambdaQueryWrapper);
28     //查询当前分类是否关联了套餐,关联就抛出异常
29     if (count2>0){
30         throw new  CustomException("当前分类关联了套餐,无法删除");
31     }
32     //正常删除分类
33     super.removeById(id);
34 }
35 
36 (其中抛出的CustomException异常也为自定义异常,在common包中创建CustomException类
37 public class CustomException extends RuntimeException{
38     public CustomException(String message){
39         super(message);
40     }
41 })

 

5.分类信息修改

1  @PutMapping
2     public R<String> update(@RequestBody Category category){
3         categoryService.updateById(category);
4         return  R.success("修改分类信息成功!");
5     }

 

posted @ 2022-10-06 15:16  赵三毛  阅读(64)  评论(0)    收藏  举报