Browse Source

新增多数据源支持

huangluyu 9 months ago
parent
commit
d149ec2c3b

+ 9 - 0
.idea/libraries/mysql_connector_java_8_0_11.xml

@@ -0,0 +1,9 @@
+<component name="libraryTable">
+  <library name="mysql-connector-java-8.0.11">
+    <CLASSES>
+      <root url="jar://$PROJECT_DIR$/lib/mysql-connector-java-8.0.11.jar!/" />
+    </CLASSES>
+    <JAVADOC />
+    <SOURCES />
+  </library>
+</component>

+ 7 - 2
src/com/huangluyu/bdmorm/bootstrap/BdmWorkSpace.java

@@ -1,9 +1,12 @@
 package com.huangluyu.bdmorm.bootstrap;
 
+import com.huangluyu.bdmorm.bootstrap.entity.DataBaseInfo;
 import com.huangluyu.bdmorm.bootstrap.entity.ModelProperty;
 import com.huangluyu.bdmorm.bootstrap.scan.BaseFileScan;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -14,9 +17,11 @@ import java.util.Map;
  */
 public class BdmWorkSpace {
 
-    public static Map<String, ModelProperty> modelInfoPool = new HashMap<>(16);
+    public static final Map<String, ModelProperty> modelInfoPool = new HashMap<>(16);
 
-    public static Map<String, String> configure = new HashMap<>(16);
+    public static final Map<String, String> configure = new HashMap<>(16);
+
+    public static final List<DataBaseInfo> dataBaseInfoList = new ArrayList<>(10);
 
     static {
         BaseFileScan.boot();

+ 35 - 0
src/com/huangluyu/bdmorm/bootstrap/db/DefaultDataSourceConnection.java

@@ -1,6 +1,16 @@
 package com.huangluyu.bdmorm.bootstrap.db;
 
+import com.huangluyu.bdmorm.bootstrap.BdmWorkSpace;
+import com.huangluyu.bdmorm.bootstrap.db.pool.BaseDataSource;
+import com.huangluyu.bdmorm.bootstrap.db.pool.DefaultDataSource;
+import com.huangluyu.bdmorm.bootstrap.entity.DataBaseInfo;
+
 import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * com.huangluyu.bdmorm.bootstrap.db
@@ -8,5 +18,30 @@ import java.sql.Connection;
  * @author 黄鲁愚
  * @date 2018/07/27
  */
+// TODO 同步控制
 public class DefaultDataSourceConnection {
+
+    private static final Map<String, BaseDataSource> dataSourceMap = new HashMap<>(16);
+
+    private static BaseDataSource defaultDataSource;
+
+    static {
+        for (DataBaseInfo dataBaseInfo : BdmWorkSpace.dataBaseInfoList) {
+            BaseDataSource dataSource = new DefaultDataSource(dataBaseInfo);
+            if (defaultDataSource == null) {
+                defaultDataSource = dataSource;
+            }
+
+            dataSourceMap.put(dataBaseInfo.dbName, dataSource);
+        }
+    }
+
+    public static BaseDataSource getDataSource(String name) {
+        return dataSourceMap.get(name);
+    }
+
+    public static BaseDataSource getDataSource() {
+        return defaultDataSource;
+    }
+
 }

+ 12 - 11
src/com/huangluyu/bdmorm/bootstrap/db/pool/DefaultDataSource.java

@@ -1,6 +1,7 @@
 package com.huangluyu.bdmorm.bootstrap.db.pool;
 
-import com.huangluyu.bdmorm.bootstrap.BdmWorkSpace;
+import com.huangluyu.bdmorm.bootstrap.entity.DataBaseInfo;
+import com.huangluyu.bdmorm.bootstrap.enums.DataBaseType;
 
 import java.io.PrintWriter;
 import java.sql.Connection;
@@ -15,31 +16,31 @@ import java.util.logging.Logger;
  * @author 黄鲁愚
  * @date 2018/07/17
  */
+// TODO 同步控制
 public class DefaultDataSource implements BaseDataSource {
-    private static DefaultDataSource defaultDataSource = new DefaultDataSource();
 
     private Connection connection;
 
-    private DefaultDataSource() {
+    private DataBaseInfo dataBaseInfo;
+
+    public DefaultDataSource(String dbName, String url, String user, String password, DataBaseType dbType) {
+        this.dataBaseInfo = new DataBaseInfo(dbName, url, user, password, dbType);
+    }
+
+    public DefaultDataSource(DataBaseInfo dataBaseInfo) {
+        this.dataBaseInfo = dataBaseInfo;
     }
 
     private Connection initConnection() throws SQLException, ClassNotFoundException {
-        String url = BdmWorkSpace.configure.get("db.url");
-        String user = BdmWorkSpace.configure.get("db.user");
-        String password = BdmWorkSpace.configure.get("db.password");
         try {
             Class.forName("com.mysql.cj.jdbc.Driver");
-            return DriverManager.getConnection(url, user, password);
+            return DriverManager.getConnection(dataBaseInfo.url, dataBaseInfo.user, dataBaseInfo.password);
         } catch (ClassNotFoundException | SQLException e) {
             System.out.println("JDBC加载错误");
             throw e;
         }
     }
 
-    public static DefaultDataSource init() {
-        return defaultDataSource;
-    }
-
     @Override
     public Connection getConnection() throws SQLException {
         if (connection == null) {

+ 30 - 0
src/com/huangluyu/bdmorm/bootstrap/entity/DataBaseInfo.java

@@ -0,0 +1,30 @@
+package com.huangluyu.bdmorm.bootstrap.entity;
+
+import com.huangluyu.bdmorm.bootstrap.enums.DataBaseType;
+
+/**
+ * com.huangluyu.bdmorm.bootstrap.entity
+ *
+ * @author 黄鲁愚
+ * @date 2018/07/14
+ */
+public class DataBaseInfo {
+
+    public final String dbName;
+
+    public final String url;
+
+    public final String user;
+
+    public final String password;
+
+    public final DataBaseType dbType;
+
+    public DataBaseInfo(String dbName, String url, String user, String password, DataBaseType dbType) {
+        this.dbName = dbName;
+        this.url = url;
+        this.user = user;
+        this.password = password;
+        this.dbType = dbType;
+    }
+}

+ 0 - 24
src/com/huangluyu/bdmorm/bootstrap/entity/DatabaseProperty.java

@@ -1,24 +0,0 @@
-package com.huangluyu.bdmorm.bootstrap.entity;
-
-import com.huangluyu.bdmorm.bootstrap.enums.DatabaseType;
-
-import java.util.Map;
-
-/**
- * com.huangluyu.bdmorm.bootstrap.entity
- *
- * @author 黄鲁愚
- * @date 2018/07/14
- */
-public class DatabaseProperty {
-
-    public DatabaseType type;
-
-    public String url;
-
-    public String username;
-
-    public String password;
-
-    public Map<String, String> info;
-}

+ 1 - 1
src/com/huangluyu/bdmorm/bootstrap/enums/DatabaseType.java

@@ -6,7 +6,7 @@ package com.huangluyu.bdmorm.bootstrap.enums;
  * @author 黄鲁愚
  * @date 2018/07/14
  */
-public enum DatabaseType {
+public enum DataBaseType {
 
     /**
      * mysql数据库

+ 14 - 7
src/com/huangluyu/bdmorm/bootstrap/scan/XmlRegister.java

@@ -1,6 +1,8 @@
 package com.huangluyu.bdmorm.bootstrap.scan;
 
 import com.huangluyu.bdmorm.bootstrap.BdmWorkSpace;
+import com.huangluyu.bdmorm.bootstrap.entity.DataBaseInfo;
+import com.huangluyu.bdmorm.bootstrap.enums.DataBaseType;
 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 
@@ -19,11 +21,13 @@ import java.io.IOException;
  */
 public class XmlRegister implements BaseFileRegister {
 
-    private static String DATA_SOURCE = "/dataSource";
+    private static final String DATA_SOURCE = "/dataSource";
 
-    private static String DB_URL = "/connection/url";
-    private static String DB_USER = "/connection/user";
-    private static String DB_PASSWORD = "/connection/password";
+    private static final String DB_TYPE = "/connection/@type";
+    private static final String DB_NAME = "/connection/name";
+    private static final String DB_URL = "/connection/url";
+    private static final String DB_USER = "/connection/user";
+    private static final String DB_PASSWORD = "/connection/password";
 
     @Override
     public void handleFile(File file) {
@@ -32,12 +36,15 @@ public class XmlRegister implements BaseFileRegister {
                 Document doc = getDocument(file.getPath());
                 XPathFactory factory = XPathFactory.newInstance();
                 XPath xpath = factory.newXPath();
+                // TODO 多数据源获取
+                String dbName = (String) xpath.compile(DATA_SOURCE + DB_NAME).evaluate(doc, XPathConstants.STRING);
                 String dbUrl = (String) xpath.compile(DATA_SOURCE + DB_URL).evaluate(doc, XPathConstants.STRING);
                 String dbUser = (String) xpath.compile(DATA_SOURCE + DB_USER).evaluate(doc, XPathConstants.STRING);
                 String dbPassword = (String) xpath.compile(DATA_SOURCE + DB_PASSWORD).evaluate(doc, XPathConstants.STRING);
-                BdmWorkSpace.configure.put("db.url", dbUrl);
-                BdmWorkSpace.configure.put("db.user", dbUser);
-                BdmWorkSpace.configure.put("db.password", dbPassword);
+                String dbType = (String) xpath.compile(DATA_SOURCE + DB_TYPE).evaluate(doc, XPathConstants.STRING);
+                DataBaseType dataBaseType = DataBaseType.valueOf(dbType.toUpperCase());
+                DataBaseInfo dataBaseInfo = new DataBaseInfo(dbName, dbUrl, dbUser, dbPassword, dataBaseType);
+                BdmWorkSpace.dataBaseInfoList.add(dataBaseInfo);
             } catch (ParserConfigurationException e) {
                 e.printStackTrace();
             } catch (IOException e) {

+ 6 - 1
src/com/huangluyu/bdmorm/facade/DB.java

@@ -1,5 +1,6 @@
 package com.huangluyu.bdmorm.facade;
 
+import com.huangluyu.bdmorm.bootstrap.db.DefaultDataSourceConnection;
 import com.huangluyu.bdmorm.bootstrap.db.pool.DefaultDataSource;
 
 import java.sql.Connection;
@@ -14,6 +15,10 @@ import java.sql.SQLException;
 public class DB {
 
     public static Connection connection() throws SQLException {
-        return DefaultDataSource.init().getConnection();
+        return DefaultDataSourceConnection.getDataSource().getConnection();
+    }
+
+    public static Connection connection(String dbName) throws SQLException {
+        return DefaultDataSourceConnection.getDataSource(dbName).getConnection();
     }
 }