news 2026/5/1 20:43:10

MybatisPlus开启多租户三步快速集成

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 */ @Bean public TenantLineInnerInterceptor tenantLineInnerInterceptor() { return new TenantLineInnerInterceptor(new TenantLineHandler() { /** * 获取租户ID * @return Expression */ @Override public Expression getTenantId() { //从请求获取登录用户的租户id String tenantId = "9527"; if (tenantId != null) { return new StringValue(tenantId); } return new NullValue(); } /** * 获取多租户的字段名 * @return String */ @Override public String getTenantIdColumn() { return tenantProperties.getColumn(); } /** * 过滤不需要根据租户隔离的表 * 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件 * @param tableName 表名 */ @Override public 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 { @Autowired private TenantLineInnerInterceptor tenantLineInnerInterceptor; @Autowired private TenantProperties tenantProperties; /** * 添加分页插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //多租户插件 if (tenantProperties.getEnable()) { interceptor.addInnerInterceptor(tenantLineInnerInterceptor); } interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加 // 这里添加 SQL 性能分析插件,用来打印 SQL 和执行时间 // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType return interceptor; } }

验证结果:调用方法查看打印sql是否携带租户字段完结撒花!!!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 20:41:32

KEGG数据库与KAAS工具在基因功能注释中的实战应用

1. KEGG数据库与KAAS工具入门指南 第一次接触基因功能注释时&#xff0c;我被各种专业术语和复杂流程搞得晕头转向。直到发现了KEGG数据库和KAAS工具这对黄金组合&#xff0c;才真正找到了高效工作的突破口。KEGG&#xff08;Kyoto Encyclopedia of Genes and Genomes&#xff…

作者头像 李华
网站建设 2026/4/12 6:47:43

微信小程序直连OneNET:基于MQTT协议的物联网数据双向通信实战

1. 为什么选择MQTT协议连接微信小程序与OneNET&#xff1f; MQTT协议在物联网领域就像快递小哥一样高效可靠。它采用发布/订阅模式&#xff0c;设备不需要知道对方在哪里&#xff0c;只需要把消息投递到指定主题&#xff08;Topic&#xff09;&#xff0c;订阅该主题的设备就能…

作者头像 李华
网站建设 2026/4/12 2:14:15

3步实现QQ空间完整备份:GetQzonehistory让数字记忆永不丢失

3步实现QQ空间完整备份&#xff1a;GetQzonehistory让数字记忆永不丢失 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory 在数字时代&#xff0c;我们的青春记忆大多沉淀在QQ空间里&#…

作者头像 李华
网站建设 2026/4/10 23:10:48

如何快速入门Reflection_Summary:AI领域10个核心概念详解

如何快速入门Reflection_Summary&#xff1a;AI领域10个核心概念详解 【免费下载链接】Reflection_Summary 算法理论基础知识应知应会 项目地址: https://gitcode.com/gh_mirrors/re/Reflection_Summary 想要在人工智能领域快速入门&#xff1f;Reflection_Summary项目为…

作者头像 李华
网站建设 2026/4/10 23:10:33

3个步骤掌握猫抓:让网页视频下载变得像呼吸一样简单

3个步骤掌握猫抓&#xff1a;让网页视频下载变得像呼吸一样简单 【免费下载链接】cat-catch 猫抓 浏览器资源嗅探扩展 / cat-catch Browser Resource Sniffing Extension 项目地址: https://gitcode.com/GitHub_Trending/ca/cat-catch 还在为无法保存喜欢的在线视频而烦…

作者头像 李华