使用MyBatis Plus自动处理创建时间和最后修改时间

作者: adm 分类: java,mybatis,mysql 发布时间: 2023-07-28

1.创建类

@Component
public class TimeMetaObjectHandler implements MetaObjectHandler {

    public static final String FIELD_CREATE_TIME = "gmtCreate";
    public static final String FIELD_UPDATE_TIME = "gmtModified";

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        this.setFieldValByName(FIELD_CREATE_TIME, now, metaObject);
        this.setFieldValByName(FIELD_UPDATE_TIME, now, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        this.setFieldValByName(FIELD_UPDATE_TIME, now, metaObject);
    }
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!