✨ 添加新特性,支持租户表配置化

This commit is contained in:
冷冷 committed 2019-01-19 23:23:23 +08:00
1 parent 4fff31a5ed
commit 2ba0ab306d
9 files changed
+31 -64

No files matched your search

@@ -23,9 +23,7 @@ import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import com.pig4cloud.pigx.common.data.datascope.DataScopeInterceptor;
import com.pig4cloud.pigx.common.data.tenant.PigxTenantConfig;
import com.pig4cloud.pigx.common.data.tenant.PigxTenantHandler;
import lombok.AllArgsConstructor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
@@ -41,11 +39,8 @@ import java.util.List;
*/
@Configuration
@MapperScan("com.pig4cloud.pigx.*.mapper")
@AllArgsConstructor
public class MybatisPlusConfig {
private final PigxTenantConfig pigxTenantConfig;
/**
* 创建租户维护处理器对象
*
@@ -54,23 +49,22 @@ public class MybatisPlusConfig {
@Bean
@ConditionalOnMissingBean
public PigxTenantHandler pigxTenantHandler() {
PigxTenantHandler pigxTenantHandler = new PigxTenantHandler();
pigxTenantHandler.setTenantTables(pigxTenantConfig.getTenantTables());
return pigxTenantHandler;
return new PigxTenantHandler();
}
/**
* 分页插件
*
* @param tenantHandler 租户处理器
* @return PaginationInterceptor
*/
@Bean
@ConditionalOnMissingBean
public PaginationInterceptor paginationInterceptor() {
public PaginationInterceptor paginationInterceptor(PigxTenantHandler tenantHandler) {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
List<ISqlParser> sqlParserList = new ArrayList<>();
TenantSqlParser tenantSqlParser = new TenantSqlParser();
tenantSqlParser.setTenantHandler(pigxTenantHandler());
tenantSqlParser.setTenantHandler(tenantHandler);
sqlParserList.add(tenantSqlParser);
paginationInterceptor.setSqlParserList(sqlParserList);
return paginationInterceptor;
@@ -1,24 +0,0 @@
package com.pig4cloud.pigx.common.data.tenant;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* 多租户自动配置类
*
* @author oathsign
*/
@AllArgsConstructor
@EnableConfigurationProperties(TenantProperties.class)
public class PigxTenantAutoConfiguration {
private final TenantProperties properties;
@Bean
PigxTenantConfig pigxTenantTable() {
PigxTenantConfig pigxTenantConfig = new PigxTenantConfig();
pigxTenantConfig.setTenantTables(properties.getTables());
return pigxTenantConfig;
}
}
@@ -1,17 +0,0 @@
package com.pig4cloud.pigx.common.data.tenant;
import lombok.Data;
/**
* 多租户配置
*
* @author oathsign
*/
@Data
public class PigxTenantConfig {
/**
* 应用了多租户的数据表集合
*/
private String[] tenantTables = new String[]{};
}
@@ -4,18 +4,26 @@ import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* 多租户配置类对应属性配置映射
* 多租户配置
*
* @author oathsign
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "pigx.tenant")
public class TenantProperties {
public class PigxTenantConfigProperties {
/**
* 服务中应用多租户的数据库表名
* 维护租户列名称
*/
private String[] tables = new String[]{};
private String column;
/**
* 多租户的数据表集合
*/
private List<String> tables = new ArrayList<>();
}
@@ -17,12 +17,12 @@
package com.pig4cloud.pigx.common.data.tenant;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author lengleng
@@ -31,9 +31,9 @@ import net.sf.jsqlparser.expression.LongValue;
* 租户维护处理器
*/
@Slf4j
@Data
public class PigxTenantHandler implements TenantHandler {
private String[] tenantTables = new String[]{};
@Autowired
private PigxTenantConfigProperties properties;
/**
* 获取租户值
@@ -54,7 +54,8 @@ public class PigxTenantHandler implements TenantHandler {
*/
@Override
public String getTenantIdColumn() {
return "tenant_id";
return StrUtil.isNotBlank(properties.getColumn())
? properties.getColumn() : "tenant_id";
}
/**
@@ -65,6 +66,6 @@ public class PigxTenantHandler implements TenantHandler {
*/
@Override
public boolean doTableFilter(String tableName) {
return !ArrayUtil.contains(tenantTables, tableName);
return !properties.getTables().contains(tableName);
}
}
@@ -2,7 +2,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.data.cache.RedisTemplateConfig,\
com.pig4cloud.pigx.common.data.cache.RedisCacheManagerConfig,\
com.pig4cloud.pigx.common.data.cache.RedisCacheAutoConfiguration,\
com.pig4cloud.pigx.common.data.tenant.PigxTenantAutoConfiguration,\
com.pig4cloud.pigx.common.data.tenant.PigxTenantConfigProperties,\
com.pig4cloud.pigx.common.data.tenant.TenantContextHolderFilter,\
com.pig4cloud.pigx.common.data.tenant.PigxFeignTenantConfiguration,\
com.pig4cloud.pigx.common.data.mybatis.MybatisPlusConfig
@@ -21,8 +21,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
*@author LCN on 2017/6/26.
*
* @author LCN on 2017/6/26.
* @author LCN
* @since 4.1.0
*/
@@ -25,7 +25,10 @@ spring:
username: root
password: root
url: jdbc:mysql://pigx-mysql:3306/pigxx_ac?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
# 租户表维护
pigx:
tenant:
column: tenant_id
tables:
- oa_leave_bill
@@ -44,8 +44,11 @@ minio:
logging:
level:
com.pig4cloud.pigx.admin.mapper: debug
# 租户表维护
pigx:
tenant:
column: tenant_id
tables:
- sys_user
- sys_role