MybatisPlus MetaObjectHandler 配置了没起作用

  • 环境
  • SpringBoot 2.2.6.RELEASE
  • Mybatis-Plus 3.3.1.tmp
  • JDK 1.8

1.添加自动填充的配置

public class MybatisPlusObjectHandler implements MetaObjectHandler{
@Override
public void insertFill(MetaObject metaObject) {
System.out.println("***************************");
System.out.println("insert fill");
System.out.println("***************************");

Object gmtCreate = getFieldValByName("gmtCreate", metaObject);

//判断表中有没有字段,有的话执行,没有就不做资源的无谓开销
boolean gmtCreate1 = metaObject.hasSetter("gmtCreate");//判断表中有没有字段,有的话执行,没有就不做资源的无谓开销
if(gmtCreate1){
System.out.println("gmtCreate="+gmtCreate);
if(gmtCreate==null){
//LocalDateTime当前时间
// LocalDateTime now = LocalDateTime.now();
setFieldValByName("gmtCreate",getDateTimeOfTimestamp(System.currentTimeMillis()),metaObject);
}
}

}

@Override
public void updateFill(MetaObject metaObject) {
System.out.println("************************");
System.out.println("update fill");
System.out.println("************************");

setFieldValByName("gmtModified",getDateTimeOfTimestamp(System.currentTimeMillis()),metaObject);
}

}

并配置到springbean里面

2.实体类添加注解

 只要这三步即可,

注意: MybatisPlusObjectHandler 类上不必加@Component标签,不然报错!
原文地址:https://www.cnblogs.com/Williamwen/p/13947785.html