【JAVA】MybatisPlus-自动填充@TableField(fill = FieldFill.*)

1.Mybatis-Plus自动填充:
FieldMetaObjectHandler实现MetaObjectHandler接口:

package com.demo.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.demo.modules.security.user.SecurityUser;
import com.demo.modules.security.user.UserDetail;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 公共字段,Mybatis-plus 自动填充值,自动更新表中的日期,用户信息
 *
 * @author system
 */
@Component
public class FieldMetaObjectHandler implements MetaObjectHandler {
    private final static String CREATE_DATE = "createDate";
    private final static String CREATOR = "creator";
    private final static String UPDATE_DATE = "updateDate";
    private final static String UPDATER = "updater";
    private final static String DEPT_ID = "deptId";


    @Override
    public void insertFill(MetaObject metaObject) {
        UserDetail user = SecurityUser.getUser();
        Date date = new Date();

        //创建者
        strictInsertFill(metaObject, CREATOR, Long.class, user.getId());
        //创建时间
        strictInsertFill(metaObject, CREATE_DATE, Date.class, date);

        //创建者所属部门
        strictInsertFill(metaObject, DEPT_ID, Long.class, user.getDeptId());

        //更新者
        strictInsertFill(metaObject, UPDATER, Long.class, user.getId());
        //更新时间
        strictInsertFill(metaObject, UPDATE_DATE, Date.class, date);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //strictUpdateFill写法在传值的时候不会生效
        //更新者
//        this.strictUpdateFill(metaObject, UPDATER, Long.class, SecurityUser.getUserId());
        this.setFieldValByName(UPDATER, SecurityUser.getUserId(), metaObject);
        //更新时间
//        this.strictUpdateFill(metaObject, UPDATE_DATE, Date.class, new Date());
        //强制更新
        this.setFieldValByName(UPDATE_DATE, new Date(), metaObject);
    }
}

2.基础实体:


package com.demo.common.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;

import java.io.Serializable;
import java.util.Date;

/**
 * 基础实体类,所有实体都需要继承
 *
 * @author system
 */
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1122L;

    /**
     * id
     */
    @TableId
    private Long id;
    /**
     * 创建者
     */
    @TableField(fill = FieldFill.INSERT)
    private Long creator;
    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createDate;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getCreator() {
        return creator;
    }

    public void setCreator(Long creator) {
        this.creator = creator;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

你可能感兴趣的:(Mybatis,java,mybatis,开发语言)