1. 程式人生 > 其它 >springboot 表格建立人,建立時間,修改人,修改時間等欄位自動填充實現程式碼

springboot 表格建立人,建立時間,修改人,修改時間等欄位自動填充實現程式碼

程式碼一

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MetaHandler implements MetaObjectHandler {

    private static final String CREATE_TIME = "createTime";
    
private static final String CREATOR_ID = "creatorId"; private static final String UPDATE_TIME = "updateTime"; private static final String UPDATE_ID = "updateId"; /** * 新增資料執行 * @param metaObject */ @Override public void insertFill(MetaObject metaObject) { Long userId
= 1L;// SecurityUtils.getUserId(); // this.setFieldValByName("createTime", new Date(), metaObject); // this.setFieldValByName("creatorId", userId, metaObject); // this.setFieldValByName("updateTime", new Date(), metaObject); // this.setFieldValByName("updateId", userId, metaObject);
if (metaObject.hasGetter(CREATOR_ID) && metaObject.getValue(CREATOR_ID) == null) { this.setFieldValByName(CREATOR_ID, userId, metaObject); } if (metaObject.hasGetter(CREATE_TIME) && metaObject.getValue(CREATE_TIME) == null) { this.setFieldValByName(CREATE_TIME, new Date(), metaObject); } } /** * 更新資料執行 * @param metaObject */ @Override public void updateFill(MetaObject metaObject) { Long userId = 10L;// SecurityUtils.getUserId(); if (metaObject.hasGetter(UPDATE_TIME) && metaObject.getValue(UPDATE_TIME) == null) { this.setFieldValByName(UPDATE_TIME, new Date(), metaObject); } if (metaObject.hasGetter(UPDATE_ID) && metaObject.getValue(UPDATE_ID) == null) { this.setFieldValByName(UPDATE_ID, userId, metaObject); } } }

程式碼二

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {
    /**
     * 自動填充功能
     * @return
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new MetaHandler());
        return globalConfig;
    }
}

程式碼三

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

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


@Data
public class BaseEntity  implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;


    @TableField(value = "creator_id", fill = FieldFill.INSERT) // 新增執行
    private Long creatorId;

    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @TableField(value = "update_id", fill = FieldFill.UPDATE) // 新增和更新執行
    private Long updateId;

    @TableField(value = "update_time", fill = FieldFill.UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }
}

程式碼四表中沒有這些欄位,過濾填充

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xxxxx.BaseEntity;
import lombok.Data;

import java.util.Date;



@Data
@TableName("xxxdemo")
public class xxxxdemo  extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /** $column.columnComment */
    @TableId(type = IdType.ASSIGN_ID)
    private Long Id;

    /** $column.columnComment */
    private Long xxxdeabc;

    /***/
    private Integer abctestType;

    /** 內容(json) */
    private String content;

    /** 描述 */
    private String description;


    /** 以下欄位,如果表中沒有就新增,有則忽略**/
    /***
     *表中不存在的欄位 修改時間
     */
    private transient  Date updateTime;
    /***
     *表中不存在的欄位 修改人
     */
    private transient   Long updateId;

    /***
     *表中不存在的欄位  建立時間
     */
    private transient  Date createTime;
    /***
     *表中不存在的欄位 建立人
     */
    private transient   Long creatorId;
}

https://blog.csdn.net/weixin_34072159/article/details/92037114

https://gitee.com/baomidou/mybatisplus-spring-boot/issues/IKFLR