首页 > 其他分享 >瑞吉外卖03

瑞吉外卖03

时间:2022-10-06 15:37:36浏览次数:73  
标签:03 LambdaQueryWrapper metaObject 分类 id 瑞吉 外卖 new public

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     }

 

标签:03,LambdaQueryWrapper,metaObject,分类,id,瑞吉,外卖,new,public
From: https://www.cnblogs.com/zhaosanmao/p/16755914.html

相关文章

  • linux下mysql常见错误(2003,1045)
    ‘2003错误’然后连接navicat会报1045,解决方法如下: 附加MySQL在linux下的大小写敏感问题解决方法: ......
  • Can't exec "mysql_config": No such file or directory at Makefile.PL line 89.
     001、问题Can'texec"mysql_config":NosuchfileordirectoryatMakefile.PLline89.  002、解决方法(base)[[email protected]]#yuminstal......
  • 03#用户管理:passwd 以及 shadow 文件
    前言passwd和shadow两个文件是用于存储用户信息的文件,包括用户名、用户ID、组ID、主目录路径等信息。userdel指令不加任何选项删除的就是这两个文件中的信息。passwd......
  • git提交报错:fatal: unable to access 'https://github.com/xxxx.git/': OpenSSL SSL_r
    一/现象  二/原因 因为git在拉取或者提交项目时,中间会有git的http和https代理,但是我们本地环境本身就有SSL协议了,所以取消git的https代理即可,不行再取消http的代......
  • Day03
    属性选择器<!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <title>Title</title>  <style>    .demoa{      float:......
  • 03 栈
    栈1.栈的定义1.1结构定义在此构建的是顺序栈,也就是说开辟一块连续的内存空间当作栈,栈只有一个入口,先进先出是它的规则一个完整的栈应该包括栈头,存放的元素和栈能够......
  • P4303 [AHOI2006]基因匹配
    初始方程为:\[f_{i,j}=\max(f_{i-1,j-1}+1,f_{i-1,j},f_{i,j-1})\]对于每一个\(i\)来说,只有五次由\(f_{i-1,j-1}\)来转移(组成DNA序列的每一种碱基在该序列中正好出......
  • POJ - 2348 Euclid's Game
    Euclid'sGame博弈很经典的博弈了首先明确每个状态必然都对应着一个局面,先手必败\(or\)先手必胜如果当前局面对于先手来说是能够选择的,也就是\(b>=a*2\),对于一......
  • day03 Jmeter授权设置
    一、BasicAuth通过验证用户名和密码才能访问资源1、添加线程组2、添加http请求请求方法get,请求路径:/basic-auth/(用户名)/(密码)3、在http下添加授权管理器,并添加参......
  • C语言 初识C语言03
    变量生活中有些值是不变的,有些值是可变的。不变的值,C语言中用常量的概念来表示,变的值C语言用变量来表示。1、定义变量的方法intage=100;floatweight=45.5f;charch='w......