Browse Source

1.动态数据源切换
2.动态添加数据源

heqinwei 7 years ago
parent
commit
c91109d4c1

+ 2 - 1
src/main/java/com/model/pojo/RepCode.java

@@ -10,7 +10,8 @@ public enum  RepCode {
     hasChartsUsing(-130, "图表正在使用"),
     hasGroupUsing(-140, "存在子分组"),
     hasConUsing(-150, "分组正在被数据源使用"),
-    hasChartUsing(-160, "分组正在被图表使用");
+    hasChartUsing(-160, "分组正在被图表使用"),
+    ChartsNameNull(-170, "表不存在");
 
     private int code;
     private String msg;

+ 3 - 3
src/main/java/com/server/DataBasesService.java

@@ -152,9 +152,9 @@ public class DataBasesService {
         dsMap.put("username", databasesInfo.getUserName());
         dsMap.put("password", databasesInfo.getPassWord());
         Map<Object, Object> target = dataSourceRegister.getTargetDataSources();
-//        if (target.size() == 0) {
-//            target.putAll(dataSourceRegister.getSlaveDataSources());
-//        }
+        if (target.size() == 0) {
+            target.putAll(dataSourceRegister.getSlaveDataSources());
+        }
         target.put(databasesInfo.getUserName(), dataSourceRegister.buildDataSource(dsMap));
         DynamicDataSource datasource = (DynamicDataSource) ContextUtil.getBean("dataSource");
         datasource.setTargetDataSources(target);

+ 8 - 2
src/main/java/com/server/ImplementSqlService.java

@@ -28,10 +28,16 @@ public class ImplementSqlService {
      */
     public RepEntity implementSql(ToSql toSql) {
         String sqlStr = toSql.getStrSql();
-        String tableName = SqlMatch.matchSql(sqlStr).toUpperCase();
+        String tableName = "";
+
+        try{
+            tableName = SqlMatch.matchSql(sqlStr).toUpperCase();
+        }catch (Exception e){
+            return new RepEntity(RepCode.ChartsNameNull);
+        }
         System.out.println(tableName);
         if ("".equals(tableName) || tableName == null) {
-            return new RepEntity(RepCode.Null);
+            return new RepEntity(RepCode.ChartsNameNull);
         }
         List<ColumnData> columnData = dataColumnMapper.getColumn(tableName);
         return new RepEntity(RepCode.success, columnData);

+ 11 - 6
src/main/java/com/util/BasesSource/DynamicDataSourceRegister.java

@@ -1,9 +1,10 @@
 package com.util.BasesSource;
 
+import com.alibaba.druid.pool.DruidDataSource;
+import com.alibaba.druid.pool.DruidDataSourceFactory;
 import org.springframework.beans.MutablePropertyValues;
 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
 import org.springframework.beans.factory.support.GenericBeanDefinition;
-import org.springframework.boot.jdbc.DataSourceBuilder;
 import org.springframework.context.EnvironmentAware;
 import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
 import org.springframework.core.env.Environment;
@@ -176,13 +177,17 @@ public class DynamicDataSourceRegister implements ImportBeanDefinitionRegistrar,
                 String url = String.valueOf(dataSourceMap.get("url"));
                 String username = String.valueOf(dataSourceMap.get("username"));
                 String password = String.valueOf(dataSourceMap.get("password"));
+                dataSourceMap.put("testWhileIdle", "true");
                 // 自定义DataSource配置
-                DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
-                        .username(username).password(password).type(dataSourceType);
-                return factory.build();
+//                DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
+//                        .username(username).password(password).type(dataSourceType);
+            DruidDataSource factory = (DruidDataSource) DruidDataSourceFactory.createDataSource(dataSourceMap);
+                return factory;
             } catch (ClassNotFoundException e) {
                 e.printStackTrace();
-            }
-            return null;
+            } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
         }
 }