Explorar o código

增加动态切换表 hsweb.datasource.table.static-mapping

zhouhao %!s(int64=6) %!d(string=hai) anos
pai
achega
03a852b921

+ 10 - 0
hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/DataSourceHolder.java

@@ -3,6 +3,7 @@ package org.hswebframework.web.datasource;
 import org.hswebframework.web.datasource.exception.DataSourceNotFoundException;
 import org.hswebframework.web.datasource.switcher.DataSourceSwitcher;
 import org.hswebframework.web.datasource.switcher.DefaultDataSourceSwitcher;
+import org.hswebframework.web.datasource.switcher.TableSwitcher;
 
 /**
  * 用于操作动态数据源,如获取当前使用的数据源,使用switcher切换数据源等
@@ -23,6 +24,8 @@ public final class DataSourceHolder {
      */
     static volatile DynamicDataSourceService dynamicDataSourceService;
 
+    static volatile TableSwitcher tableSwitcher;
+
     public static void checkDynamicDataSourceReady() {
         if (dynamicDataSourceService == null) {
             throw new UnsupportedOperationException("dataSourceService not ready");
@@ -36,6 +39,13 @@ public final class DataSourceHolder {
         return dataSourceSwitcher;
     }
 
+    /**
+     * @return 表切换器, 用于动态切换系统功能表
+     */
+    public static TableSwitcher tableSwitcher() {
+        return tableSwitcher;
+    }
+
     /**
      * @return 默认数据源
      */

+ 31 - 14
hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/DynamicDataSourceAutoConfiguration.java

@@ -6,11 +6,14 @@ import org.hswebframework.web.datasource.config.InSpringDynamicDataSourceConfig;
 import org.hswebframework.web.datasource.service.InSpringContextDynamicDataSourceService;
 import org.hswebframework.web.datasource.service.InSpringDynamicDataSourceConfigRepository;
 import org.hswebframework.web.datasource.switcher.DataSourceSwitcher;
+import org.hswebframework.web.datasource.switcher.DefaultTableSwitcher;
+import org.hswebframework.web.datasource.switcher.TableSwitcher;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
@@ -21,7 +24,7 @@ import javax.sql.DataSource;
  */
 @Configuration
 @ImportAutoConfiguration(AopDataSourceSwitcherAutoConfiguration.class)
-public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
+public class DynamicDataSourceAutoConfiguration {
 
     @Bean
     @ConditionalOnMissingBean(SqlExecutor.class)
@@ -29,6 +32,12 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
         return new DefaultJdbcExecutor();
     }
 
+    @Bean
+    @ConfigurationProperties(prefix = "hsweb.datasource.table")
+    public DefaultTableSwitcher defaultTableSwitcher() {
+        return new DefaultTableSwitcher();
+    }
+
     @Bean
     @ConditionalOnMissingBean(DynamicDataSourceConfigRepository.class)
     public InSpringDynamicDataSourceConfigRepository inSpringDynamicDataSourceConfigRepository() {
@@ -43,20 +52,28 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
         return new InSpringContextDynamicDataSourceService(repository, dataSourceProxy);
     }
 
-    @Override
-    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
-        return bean;
-    }
+    @Bean
+    public BeanPostProcessor switcherInitProcessor() {
+        return new BeanPostProcessor() {
+            @Override
+            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+                return bean;
+            }
 
-    @Override
-    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
-        if (bean instanceof DynamicDataSourceService) {
-            DataSourceHolder.dynamicDataSourceService = ((DynamicDataSourceService) bean);
-        }
-        if (bean instanceof DataSourceSwitcher) {
-            DataSourceHolder.dataSourceSwitcher = ((DataSourceSwitcher) bean);
-        }
-        return bean;
+            @Override
+            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+                if (bean instanceof DynamicDataSourceService) {
+                    DataSourceHolder.dynamicDataSourceService = ((DynamicDataSourceService) bean);
+                }
+                if (bean instanceof DataSourceSwitcher) {
+                    DataSourceHolder.dataSourceSwitcher = ((DataSourceSwitcher) bean);
+                }
+                if (bean instanceof TableSwitcher) {
+                    DataSourceHolder.tableSwitcher = ((TableSwitcher) bean);
+                }
+                return bean;
+            }
+        };
     }
 
 

+ 36 - 0
hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/switcher/DefaultTableSwitcher.java

@@ -0,0 +1,36 @@
+package org.hswebframework.web.datasource.switcher;
+
+import lombok.extern.slf4j.Slf4j;
+import org.hswebframework.web.ThreadLocalUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author zhouhao
+ * @since 3.0.0-RC
+ */
+public class DefaultTableSwitcher implements TableSwitcher {
+
+    private Map<String, String> staticMapping = new HashMap<>();
+
+    @Override
+    public void use(String source, String target) {
+        getMapping().put(source, target);
+    }
+
+    private Map<String, String> getMapping() {
+        return ThreadLocalUtils.get(DefaultTableSwitcher.class.getName() + "_current", HashMap::new);
+    }
+
+    @Override
+    public String getTable(String name) {
+        return getMapping()
+                .getOrDefault(name, staticMapping.getOrDefault(name, name));
+    }
+
+    @Override
+    public void reset() {
+        ThreadLocalUtils.remove(DefaultTableSwitcher.class.getName() + "_current");
+    }
+}

+ 15 - 0
hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/switcher/TableSwitcher.java

@@ -0,0 +1,15 @@
+package org.hswebframework.web.datasource.switcher;
+
+/**
+ * 表切换器
+ *
+ * @author zhouhao
+ * @since 3.0.0-RC
+ */
+public interface TableSwitcher {
+    void use(String source, String target);
+
+    String getTable(String name);
+
+    void reset();
+}