mybatis-plus自动填充功能
如果需要用到自动填充功能,比如自动填充数据的新增日期,修改日期。先在config包下新增一个配置文件MyMetaObjectHandler
package com.xzit.config; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.Date; @Slf4j @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { log.info("start insert fill ...."); this.strictInsertFill(metaObject, "createTime", () -> new Date(), Date.class); // 起始版本 3.3.3(推荐) } @Override public void updateFill(MetaObject metaObject) { } }
然后在实体类里想要自动填充的字段上面加上
@TableField(fill = FieldFill.INSERT)
注解即可
package com.xzit.entity; import com.baomidou.mybatisplus.annotation.*; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * * @TableName member */ @TableName(value ="member") @Data public class Member implements Serializable { /** * */ @TableId(type = IdType.AUTO) private Integer id; /** * 会员姓名 */ private String memberName; /** * 会员等级 */ private Integer level; /** * 入会时间 */ @TableField(fill = FieldFill.INSERT) private Date createTime; /** * 是否删除 */ private Integer deleted; @TableField(exist = false) private static final long serialVersionUID = 1L; }