MyBatis-Plus-實用的功能自動填充欄位

_Phoenix發表於2024-07-05

前言:

java專案用到了mybatis-plus,在一些類裡面需要在更新時候,統一設定,修改人,修改ID,修改時間。新增時候設定 建立人,建立時間等

基礎類:

@Data
public abstract class BaseModel implements Serializable {
    /**
     * 邏輯刪除
     */
    @TableField(value = "is_delete", fill = FieldFill.INSERT)
    @TableLogic
    @ApiModelProperty(hidden = true)
    protected Integer deleted = 0;
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(hidden = true)
    protected String createId;
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(hidden = true)
    protected String createName;
    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(hidden = true)
    protected Date createDate;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(hidden = true)
    protected String modifiedId;
    @TableField(fill = FieldFill.INSERT_UPDATE,updateStrategy = FieldStrategy.NOT_NULL)
    @ApiModelProperty(hidden = true)
    protected String modifiedName;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(hidden = true)
    protected Date modifiedDate;

}

然後,每個業務類都要繼承這個基礎類:確保資料庫欄位也都有這些欄位。比如:

@Data
public class ErpSaleOrderTransactionBill extends BaseModel {
    private static final long serialVersionUID = 561288556671900702L;
    /**
    * 主鍵id
    */
    @TableId(type = IdType.ASSIGN_ID)
    @NotEmpty
    private String id;
                
    /**
    * 賬單號
    */  
    private String orderCode;
                
    /**
    * 單據類別
    */  
    private String confOrderCategory;
}

mybatis-plus框架,我們可以使用: ,這裡我們可以不設定, modifiedName,modifiedDate等

updateBatchById(calculateRiskLevelListUpdate);

最重要的:mybatis-plus攔截執行

package com.alpha.erp.config;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.orderplus.core.auth.Audience;
import com.orderplus.core.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
@Component
public class MetaDataObjectHandler implements MetaObjectHandler {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private Audience audience;


    /**
     * 獲取請求引數 - 使用者ID
     */
    protected String getUserId() {
        try {
            String token = request.getHeader(HttpHeaders.AUTHORIZATION);
            if (StringUtils.isEmpty(token)) {
                return "";
            }
            return JwtTokenUtil.getUserId(token.substring(7), audience.getBase64Secret());
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 獲取請求引數 - 使用者ID
     */
    protected String getUsername() {
        try {
            String token = request.getHeader(HttpHeaders.AUTHORIZATION);
            if (StringUtils.isEmpty(token)) {
                return "";
            }
            return JwtTokenUtil.getUsername(token.substring(7), audience.getBase64Secret());
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 獲取請求引數 - 集團程式碼
     */
    protected String getEnterpriseNumber() {
        try {
            String token = request.getHeader(HttpHeaders.AUTHORIZATION);
            if (StringUtils.isEmpty(token)) {
                return "";
            }
            return JwtTokenUtil.getEnterpriseNumber(token.substring(7), audience.getBase64Secret());
        } catch (Exception e) {
            return "";
        }
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
//        this.strictInsertFill(metaObject, "createId", String.class, getUserId());
        this.strictInsertFill(metaObject, "createDate", Date.class, new Date());
//        this.strictInsertFill(metaObject, "createName", String.class, getUsername());
//        this.strictInsertFill(metaObject, "modifiedId", String.class, getUserId());
        this.strictInsertFill(metaObject, "modifiedDate", Date.class, new Date());
//        this.strictInsertFill(metaObject, "modifiedName", String.class, getUsername());
        this.strictInsertFill(metaObject, "groupCode", String.class, getEnterpriseNumber());
        this.strictInsertFill(metaObject, "brand", String.class, getEnterpriseNumber());

        this.fillStrategy(metaObject, "createId", getUserId());
        this.fillStrategy(metaObject, "createName", getUsername());
        this.fillStrategy(metaObject, "modifiedId", getUserId());
        this.fillStrategy(metaObject, "modifiedName", getUsername());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
//        metaObject.setValue("modifiedDate",null);
//        metaObject.setValue("modifiedId",null);
//        metaObject.setValue("modifiedName",null);
//        this.strictUpdateFill(metaObject, "modifiedDate", Date.class, new Date());
//        this.strictUpdateFill(metaObject, "modifiedId", String.class, getUserId());
//        this.strictUpdateFill(metaObject, "modifiedName", String.class, getUsername());

        metaObject.setValue("modifiedDate",null);
        this.strictUpdateFill(metaObject, "modifiedDate", Date.class, new Date());
        if(StrUtil.isNotBlank(getUserId())){
            metaObject.setValue("modifiedId",null);
            this.strictUpdateFill(metaObject, "modifiedId", String.class, getUserId());
        }
        if(StrUtil.isNotBlank(getUsername())){
            metaObject.setValue("modifiedName",null);
            this.strictUpdateFill(metaObject, "modifiedName", String.class, getUsername());
        }
    }
}

然後你可以看到,執行時候會自動賦值。

相關文章