MybatisPlus开启多租户三步快速集成
前言
记录mybatisPlus集成多租户实现数据逻辑隔离
步骤一
yml添加配置:标红字段名取决设置表的租户字段
tenant:# 是否开启租户模式enable: true# 需要排除的多租户的表exclusionTable:- "dp_im_user_friend"# 租户字段名称column: tenant_id
步骤二
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;import java.util.List;/*** 白名单配置*/
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "tenant")
public class TenantProperties {/*** 是否开启租户模式*/private Boolean enable;/*** 多租户字段名称*/private String column;/*** 需要排除的多租户的表*/private List<String> exclusionTable;
}
注:getTenantId()方法 从当前请求上下文中获取租户ID ,这里简单返回固定值9527,实际应从用户会话中获取
package com.macro.mall.config.tenant;import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.macro.mall.config.MybatisPlusConfig;
import lombok.RequiredArgsConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 多租户配置中心*/
@Configuration
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@AutoConfigureBefore(MybatisPlusConfig.class)
public class TenantConfig {private final TenantProperties tenantProperties;/*** 新多租户插件配置,一缓和二缓遵循mybatis的规则,* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false* 避免缓存万一出现问题** @return TenantLineInnerInterceptor*/@Beanpublic TenantLineInnerInterceptor tenantLineInnerInterceptor() {return new TenantLineInnerInterceptor(new TenantLineHandler() {/*** 获取租户ID* @return Expression*/@Overridepublic Expression getTenantId() {//从请求获取登录用户的租户idString tenantId = "9527";if (tenantId != null) {return new StringValue(tenantId);}return new NullValue();}/*** 获取多租户的字段名* @return String*/@Overridepublic String getTenantIdColumn() {return tenantProperties.getColumn();}/*** 过滤不需要根据租户隔离的表* 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件* @param tableName 表名*/@Overridepublic boolean ignoreTable(String tableName) {return tenantProperties.getExclusionTable().stream().anyMatch((t) -> t.equalsIgnoreCase(tableName));}});}
}
步骤三
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.macro.mall.config.tenant.TenantProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author luoyq* @date 2025/3/14 22:15* @description:*/
@Configuration
public class MybatisPlusConfig {@Autowiredprivate TenantLineInnerInterceptor tenantLineInnerInterceptor;@Autowiredprivate TenantProperties tenantProperties;/*** 添加分页插件*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//多租户插件if (tenantProperties.getEnable()) {interceptor.addInnerInterceptor(tenantLineInnerInterceptor);}interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加// 这里添加 SQL 性能分析插件,用来打印 SQL 和执行时间// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbTypereturn interceptor;}}
验证结果:调用方法查看打印sql是否携带租户字段完结撒花!!!