MyBatis Plus MetaObjectHandler多租户增改自动填充租户ID

本文介绍如何使用SpringBoot与MyBatisPlus实现多租户环境下自动填充租户ID,通过自定义MyMetaObjectHandler来处理实体类中的字段自动填充,包括创建和更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多租户系统增改自动填充租户ID

多租户系统根据租户ID来进行数据隔离的情况下,整个系统的mapper层有很多需要注意租户ID的判断,增改都需要考虑到租户ID

此实例基于springboot + mybatis plus实现

直接上代码~

MyMetaObjectHander 自动填充设置类


import cn.shopex.cloud.model.product.constant.FieldNameConstant;
import cn.shopex.cloud.model.product.constant.NumberConstant;
import cn.shopex.cloud.product.util.ContextInfo;
import cn.shopex.cloud.product.util.ContextInfoUtil;
import cn.shopex.cloud.product.util.GlobalPlatformGenerator;
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;

/**
 * @author 
 * @create 
 * @desc mybatis-plus 自定义元数据填充Handler
 **/
@Component
@Slf4j
public class MyMetaObjectHander implements MetaObjectHandler {


    /**
     * 插入自动填充字段
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        ContextInfo contextInfo = ContextInfoUtil.getContext();
        if(metaObject.hasGetter(FieldNameConstant.UUID)){
            this.setFieldValByName(FieldNameConstant.UUID, GlobalPlatformGenerator.generatorId(), metaObject);
        }
        if(metaObject.hasGetter(FieldNameConstant.TENANT_ID_HUMP)){
            this.setFieldValByName(FieldNameConstant.TENANT_ID_HUMP, contextInfo.getTenantId(), metaObject);
        }
        this.setFieldValByName(FieldNameConstant.CREATED_AT_HUMP, LocalDateTime.now(), metaObject);
        this.setFieldValByName(FieldNameConstant.UPDATED_AT_HUMP, LocalDateTime.now(), metaObject);
        if(contextInfo != null){
            this.setFieldValByName(FieldNameConstant.CREATED_BY_HUMP, contextInfo.getUser(), metaObject);
            this.setFieldValByName(FieldNameConstant.UPDATED_BY_HUMP, contextInfo.getUser(), metaObject);
        }
        this.setFieldValByName(FieldNameConstant.IS_DELETED_HUMP, NumberConstant.ZERO_STRING, metaObject);
        log.info("come to insert fill .........");
    }

    /**
     * 更新字段填充字段
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        ContextInfo contextInfo = ContextInfoUtil.getContext();
        this.setFieldValByName(FieldNameConstant.UPDATED_AT_HUMP, LocalDateTime.now(), metaObject);
        if(contextInfo != null) {
            this.setFieldValByName(FieldNameConstant.UPDATED_BY_HUMP, contextInfo.getUser(), metaObject);
        }
        log.info("come to update fill .......");
    }
}

应用实体类




import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author 
 * @create 2021-05-07
 * @desc 
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("product")
public class ProductDO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * uuid
     */
    @TableField(fill = FieldFill.INSERT)
    private String uuid;

    /**
     * 租户id
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String tenantId;
	
	@TableField(fill = FieldFill.INSERT)
    @TableLogic
    @ApiModelProperty(value = "是否删除", notes = "是否删除")
    private String isDeleted;

    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间", notes = "创建时间")
    private LocalDateTime createdAt;

    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建人", notes = "创建人")
    private String createdBy;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(value = "更新时间", notes = "更新时间")
    private LocalDateTime updatedAt;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(value = "更新人", notes = "更新人")
    private String updatedBy;

}

@TableField

fill = FieldFill.INSERT

inser语句时自动填充该字段数据

fill = FieldFill.UPDATE

update语句时自动填充该字段数据

fill = FieldFill.INSERT_UPDATE

update语句或insert语句时自动填充该字段数据

保存上下文租户实体信息及工具类

/**
 * @author 
 * @create  
 * @desc 租户上下文信息
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ContextInfo {

    /**
     * 租户id
     */
    private String tenantId;

    /**
     * 当前用户
     */
    private String user;

    /**
     * 当前语言
     */
    private String languageTag;

}
package cn.shopex.cloud.product.util;

/**
 * @author
 * @create 
 * @desc 上下文信息保存工具类
 **/
public class ContextInfoUtil {

    private ContextInfoUtil() {
    }

    private static final ThreadLocal<ContextInfo> CONTEXT_INFO_HOLDER = new ThreadLocal<>();

    public static void setContext(ContextInfo contextInfo) {
        CONTEXT_INFO_HOLDER.set(contextInfo);
    }

    public static ContextInfo getContext() {
        return CONTEXT_INFO_HOLDER.get();
    }

    public static void clearContext() {
        CONTEXT_INFO_HOLDER.remove();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值