MybatisPlus公共字段自动填充:


首先在实体类字段上标识注解:
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
这里需要关注如何在非Controller类里获取session并最终获得UserId,使用ThreadLocal<Long>


创建ThreadLocal管理类:
// BaseContext.java
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();
}
}
补充自动填充操作
创建元对象处理类:
// MyMetaObjectHandler.java
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());
}
}
记得要将Controller里的设置createTime, updateTime, createUser, updateUser的代码去掉
新增分类:


// CategoryController.java
@PostMapping
public R<String> save(@RequestBody Category category){
if(categoryService.save(category)) return R.success("save success...");
else return R.error("save fail...");
}
分页查询:
注意:资源里的category表结构中没有is_delete字段,而实体类中有,所以需要自行添加
// CategoryController.java
@GetMapping("/page")
public R<Page<Category>> page(int page, int pageSize){
Page<Category> pageInfo = new Page<>(page, pageSize);
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Category::getUpdateTime);
categoryService.page(pageInfo, queryWrapper);
if (pageInfo.getTotal() == 0) return R.error("no field");
else return R.success(pageInfo);
}
删除分类:
仅仅只是简单地实现了deleteById时不够的,还需要检测分类是否关联了菜品和套餐

所以在service里添加一个方法,负责在删除分类前检测是否关联了菜品和套餐
// CategoryService.java
boolean remove(Long id);
当发现有关联时抛出一个自定义的异常:
// CategoryServiceImpl.java
@Resource
private DishService dishService;
@Resource
private SetMealService setMealService;
@Override
public boolean remove(Long id) {
LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
dishQueryWrapper.eq(Dish::getCategoryId, id);
int dishCount = dishService.count(dishQueryWrapper);
if (dishCount > 0){
throw new CustomException("this category related Dish...");
}
LambdaQueryWrapper<SetMeal> setMealQueryWrapper = new LambdaQueryWrapper<>();
setMealQueryWrapper.eq(SetMeal::getCategoryId, id);
int setMealCount = setMealService.count(setMealQueryWrapper);
if (setMealCount > 0){
throw new CustomException("this category related SetMeal...");
}
return super.removeById(id);
}
// CustomException.java
public class CustomException extends RuntimeException {
public CustomException(String message){
super(message);
}
}
// CategoryController.java
@DeleteMapping
public R<String> delete(Long ids){
if (categoryService.remove(ids)) return R.success("delete success...");
else return R.error("delete fail...");
}
修改分类:
// CategoryController.java
@PostMapping
public R<String> update(@RequestBody Category category){
if(categoryService.updateById(category)) return R.success("update success...");
else return R.error("update fail...");
}
这里有个问题:我修改和新增分类时没有自动填充字段导致更新的记录的更新时间等为空,数据库报错,但是我进行员工更新时却正常
解决:MyMetaObjectHandler类需要添加@Component注解,让它加入IoC容器,这样才能正常使用