Przeglądaj źródła

:sparkles: 应用多租户的表在对应服务的配置文件中进行配置

oathsign 6 lat temu
rodzic
commit
cfc9114780

+ 19 - 1
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/mybatis/MybatisPlusConfig.java

@@ -23,7 +23,9 @@ 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;
@@ -39,8 +41,24 @@ import java.util.List;
  */
 @Configuration
 @MapperScan("com.pig4cloud.pigx.*.mapper")
+@AllArgsConstructor
 public class MybatisPlusConfig {
 
+	private final PigxTenantConfig pigxTenantConfig;
+
+	/**
+	 * 创建租户维护处理器对象
+	 *
+	 * @return 处理后的租户维护处理器
+	 */
+	@Bean
+	@ConditionalOnMissingBean
+	public PigxTenantHandler pigxTenantHandler() {
+		PigxTenantHandler pigxTenantHandler = new PigxTenantHandler();
+		pigxTenantHandler.setTenantTables(pigxTenantConfig.getTenantTables());
+		return pigxTenantHandler;
+	}
+
 	/**
 	 * 分页插件
 	 *
@@ -52,7 +70,7 @@ public class MybatisPlusConfig {
 		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
 		List<ISqlParser> sqlParserList = new ArrayList<>();
 		TenantSqlParser tenantSqlParser = new TenantSqlParser();
-		tenantSqlParser.setTenantHandler(new PigxTenantHandler());
+		tenantSqlParser.setTenantHandler(pigxTenantHandler());
 		sqlParserList.add(tenantSqlParser);
 		paginationInterceptor.setSqlParserList(sqlParserList);
 		return paginationInterceptor;

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

@@ -0,0 +1,24 @@
+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;
+	}
+}

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

@@ -0,0 +1,17 @@
+package com.pig4cloud.pigx.common.data.tenant;
+
+import lombok.Data;
+
+/**
+ * 多租户配置
+ *
+ * @author oathsign
+ */
+@Data
+public class PigxTenantConfig {
+
+	/**
+	 * 应用了多租户的数据表集合
+	 */
+	private String[] tenantTables = new String[]{};
+}

+ 4 - 3
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/PigxTenantHandler.java

@@ -19,6 +19,7 @@ package com.pig4cloud.pigx.common.data.tenant;
 
 import cn.hutool.core.util.ArrayUtil;
 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;
@@ -30,9 +31,9 @@ import net.sf.jsqlparser.expression.LongValue;
  * 租户维护处理器
  */
 @Slf4j
+@Data
 public class PigxTenantHandler implements TenantHandler {
-	private static final String[] TENANT_TABLES = new String[]{"sys_user", "sys_role", "sys_dept",
-			"sys_log", "sys_social_details", "sys_dict", "sys_log", "oa_leave_bill"};
+	private String[] tenantTables = new String[]{};
 
 	/**
 	 * 获取租户值
@@ -64,6 +65,6 @@ public class PigxTenantHandler implements TenantHandler {
 	 */
 	@Override
 	public boolean doTableFilter(String tableName) {
-		return !ArrayUtil.contains(TENANT_TABLES, tableName);
+		return !ArrayUtil.contains(tenantTables, tableName);
 	}
 }

+ 21 - 0
pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/TenantProperties.java

@@ -0,0 +1,21 @@
+package com.pig4cloud.pigx.common.data.tenant;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 多租户配置类对应属性配置映射
+ *
+ * @author oathsign
+ */
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "pigx.tenant")
+public class TenantProperties {
+
+	/**
+	 * 服务中应用多租户的数据库表名
+	 */
+	private String[] tables = new String[]{};
+}

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

@@ -2,6 +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.TenantContextHolderFilter,\
     com.pig4cloud.pigx.common.data.tenant.PigxFeignTenantConfiguration,\
     com.pig4cloud.pigx.common.data.mybatis.MybatisPlusConfig

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

@@ -25,3 +25,7 @@ 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:
+    tables:
+      - oa_leave_bill

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

@@ -44,3 +44,13 @@ minio:
 logging:
   level:
     com.pig4cloud.pigx.admin.mapper: debug
+pigx:
+  tenant:
+    tables:
+      - sys_user
+      - sys_role
+      - sys_dept
+      - sys_log
+      - sys_social_details
+      - sys_dict
+      - sys_log