公共字段自动填充

代码实现

1.
@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;
2.
time我们可以用LocalDateTime方法,但是ID 我们需要动态接收,MetaObjectHandler不支持获得HttpSession
可以使用ThreadLocal来解决此问题,它是JDK中提供的一个类。

先在过滤器类用set方法把id存进去
再在metaObjectHandler的get方法取出id然后设置
编写BaseContext工具类,基于ThreadLocal封装的工具类
public class BaseContext {
private static ThreadLocal<Long> threadLocal =new ThreadLocal<>();
public static void setCurrentId(Long id){
threadLocal.set(id);
}
public static Long getCurrentId(){
return threadLocal.get();
}
}
在LoginCheckFilter的doFilter方法中调用BaseContext来设置当前登录用户的id
在MyMetaObjectHandler的方法中调用BaseContext获取登录用户的id
最终MyMetaObjectHandler:
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
//插入的时候自动填充
@Override
public void insertFill(MetaObject metaObject) {
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("createUser", BaseContext.getCurrentId());
metaObject.setValue("updateUser", BaseContext.getCurrentId());
}
@Override
public void updateFill(MetaObject metaObject) {
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("updateUser", BaseContext.getCurrentId());
}
}
新增分类


@PostMapping
public R save(HttpServletRequest request, @RequestBody Category category){
categoryService.save(category);
return new R(1,"添加成功");
}
分类信息分页查询
@GetMapping("/page")
public R page(int page,int pageSize){
Page<Category> pageInfo =new Page<>(page,pageSize);
LambdaQueryWrapper<Category> queryWrapper =new LambdaQueryWrapper<>();
queryWrapper.orderByAsc(Category::getSort);
categoryService.page(pageInfo,queryWrapper);
return new R(1,"查询成功",pageInfo);
}
删除分类(当分类关联了菜品或者套餐时,此分类不允许删除)


自定业务异常
/**
* 自定义业务异常
*/
public class CustomException extends RuntimeException{
public CustomException(String message){
super(message);
}
}
CategoryServiceImpl:
@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);
long count1 = dishService.count(dishLambdaQueryWrapper);
//查询当前分类是否已关联菜品,如果已经关联,抛出一个业务异常
if(count1>0){
//已经关联菜品,抛出一个业务异常
throw new CustomException("当前分类下关联了菜品,不能删除");
}
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper =new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
long count2 = setmealService.count(setmealLambdaQueryWrapper);
//查询当前分类是否已关联套餐,如果已经关联,抛出一个业务异常
if (count2>0){
//已经关联套餐,抛出一个业务异常
throw new CustomException("当前分类下关联了套餐,不能删除");
}
//最终执行删除操作
super.removeById(id);
}
在我们之前写的GlobalExceptionHandler下写异常handler
@ExceptionHandler(CustomException.class)
public R exceptionHandler(CustomException ex){
log.error(ex.getMessage());
//返回失败信息
return new R(0,ex.getMessage());
}

![[附源码]Python计算机毕业设计Django少儿节目智能推荐系统](https://img-blog.csdnimg.cn/8aefdd883efb48be8bba815721aacdb7.png)
















![[附源码]Node.js计算机毕业设计公司办公自动化系统Express](https://img-blog.csdnimg.cn/f5cf70d723ba4b93b0fe037666169b98.png)
