增加多租户功能(应用隔离)
This commit is contained in:
+26
-1
@@ -1,7 +1,14 @@
|
||||
package com.central.db.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
|
||||
|
||||
import com.central.common.properties.TenantProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
@@ -13,12 +20,30 @@ import org.springframework.context.annotation.Profile;
|
||||
*/
|
||||
@Import(DateMetaObjectHandler.class)
|
||||
public class DefaultMybatisPlusConfig {
|
||||
@Autowired
|
||||
private TenantHandler tenantHandler;
|
||||
|
||||
@Autowired
|
||||
private ISqlParserFilter sqlParserFilter;
|
||||
|
||||
@Autowired
|
||||
private TenantProperties tenantProperties;
|
||||
|
||||
/**
|
||||
* 分页插件,自动识别数据库类型
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
return new PaginationInterceptor();
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
boolean enableTenant = tenantProperties.getEnable();
|
||||
//是否开启多租户隔离
|
||||
if (enableTenant) {
|
||||
TenantSqlParser tenantSqlParser = new TenantSqlParser()
|
||||
.setTenantHandler(tenantHandler);
|
||||
paginationInterceptor.setSqlParserList(CollUtil.toList(tenantSqlParser));
|
||||
paginationInterceptor.setSqlParserFilter(sqlParserFilter);
|
||||
}
|
||||
return paginationInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.central.db.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
|
||||
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
|
||||
import com.central.common.utils.TenantContextHolder;
|
||||
import com.central.common.properties.TenantProperties;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.NullValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* 多租户自动配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
@EnableConfigurationProperties(TenantProperties.class)
|
||||
public class TenantAutoConfigure {
|
||||
@Autowired
|
||||
private TenantProperties tenantProperties;
|
||||
|
||||
@Bean
|
||||
public TenantHandler tenantHandler() {
|
||||
return new TenantHandler() {
|
||||
/**
|
||||
* 获取租户id
|
||||
*/
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
String tenant = TenantContextHolder.getTenant();
|
||||
if (tenant != null) {
|
||||
return new StringValue(TenantContextHolder.getTenant());
|
||||
}
|
||||
return new NullValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取租户列名
|
||||
*/
|
||||
@Override
|
||||
public String getTenantIdColumn() {
|
||||
return "tenant_id";
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤不需要根据租户隔离的表
|
||||
* @param tableName 表名
|
||||
*/
|
||||
@Override
|
||||
public boolean doTableFilter(String tableName) {
|
||||
return tenantProperties.getIgnoreTables().stream().anyMatch(
|
||||
(e) -> e.equalsIgnoreCase(tableName)
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤不需要根据租户隔离的MappedStatement
|
||||
*/
|
||||
@Bean
|
||||
public ISqlParserFilter sqlParserFilter() {
|
||||
return metaObject -> {
|
||||
MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
|
||||
return tenantProperties.getIgnoreSqls().stream().anyMatch(
|
||||
(e) -> e.equalsIgnoreCase(ms.getId())
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.db.config.TenantAutoConfigure
|
||||
Reference in New Issue
Block a user