多租户代码框架
This commit is contained in:
@@ -2,35 +2,82 @@ package com.rj.config;
|
||||
|
||||
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.handler.TenantLineHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.rj.tenant.TenantContextHolder;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* MyBatis Plus 配置类
|
||||
*
|
||||
* @author 李中华
|
||||
* @since 2025-08-08
|
||||
*
|
||||
* <p>包含:</p>
|
||||
* <ul>
|
||||
* <li>多租户拦截器:根据当前线程中的租户ID自动为SQL拼接 tenant_id 条件;</li>
|
||||
* <li>分页插件;</li>
|
||||
* <li>乐观锁插件。</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 分页插件配置
|
||||
* MyBatis-Plus 主拦截器配置
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(TenantLineHandler tenantLineHandler) {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
|
||||
// 分页插件
|
||||
|
||||
// 1. 多租户插件(需优先添加)
|
||||
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantLineHandler));
|
||||
|
||||
// 2. 分页插件
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
|
||||
// 乐观锁插件
|
||||
|
||||
// 3. 乐观锁插件
|
||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||
|
||||
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 多租户处理器:定义如何获取租户ID、租户字段名以及忽略的表
|
||||
*/
|
||||
@Bean
|
||||
public TenantLineHandler tenantLineHandler() {
|
||||
// 需要忽略多租户的表(不自动拼接 tenant_id)
|
||||
Set<String> ignoreTables = new HashSet<>();
|
||||
ignoreTables.add("tenant"); // 租户表本身
|
||||
ignoreTables.add("industry_tags"); // 行业标签(示例:如认为是公共字典)
|
||||
|
||||
return new TenantLineHandler() {
|
||||
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
String tenantId = TenantContextHolder.getTenantId();
|
||||
// 没有租户ID时,返回 null 由 MyBatis-Plus 决定处理策略,通常为不过滤或抛错(视版本而定)
|
||||
return tenantId == null ? null : new StringValue(tenantId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTenantIdColumn() {
|
||||
return "tenant_id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
// 忽略名单内的表不做租户隔离
|
||||
return ignoreTables.contains(tableName);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user