Ver código fonte

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

冷冷 6 anos atrás
pai
commit
2ba0ab306d

+ 4 - 10
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/mybatis/MybatisPlusConfig.java

@@ -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;

+ 0 - 24
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/PigxTenantAutoConfiguration.java

@@ -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;
-	}
-}

+ 0 - 17
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/PigxTenantConfig.java

@@ -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[]{};
-}

+ 12 - 4
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/TenantProperties.java

@@ -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 column;
 
 	/**
-	 * 服务中应用多租户的数据库表名
+	 * 多租户的数据表集合
 	 */
-	private String[] tables = new String[]{};
+	private List<String> tables = new ArrayList<>();
 }

+ 7 - 6
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/PigxTenantHandler.java

@@ -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);
 	}
 }

+ 1 - 1
pigx-common/pigx-common-data/src/main/resources/META-INF/spring.factories

@@ -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

+ 1 - 2
pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/TransactionConfiguration.java

@@ -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
  */

+ 3 - 0
pigx-config/src/main/resources/config/pigx-activiti-dev.yml

@@ -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

+ 3 - 0
pigx-config/src/main/resources/config/pigx-upms-biz-dev.yml

@@ -44,8 +44,11 @@ minio:
 logging:
   level:
     com.pig4cloud.pigx.admin.mapper: debug
+
+# 租户表维护
 pigx:
   tenant:
+    column: tenant_id
     tables:
       - sys_user
       - sys_role