1、首先要配置一个自动填充处理器
package com.it.admin.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
// 创建时间
this.setFieldValByName("createdTime", LocalDateTime.now(), metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
// 更新时间
this.setFieldValByName("lastModifyTime", LocalDateTime.now(), metaObject);
}
}
2、要在pojo中配置
@ApiModelProperty(value = "创建时间")
@TableField(value = "created_time",fill = FieldFill.INSERT)
private LocalDateTime createdTime;
@ApiModelProperty(value = "更新时间")
@TableField(value = "lastModify_time",fill = FieldFill.INSERT)
private LocalDateTime lastModifyTime;
要注意:创建的时间类型是LocalDateTime,那么对应的自动填充处理器中就是LocalDateTime.now(),不一致则不成功