Browse Source

完善多数据源获取

huangluyu 1 year ago
parent
commit
8ad1817255

+ 32 - 23
src/com/huangluyu/bdmorm/bootstrap/scan/XmlRegister.java

@@ -4,6 +4,8 @@ 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.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -12,6 +14,7 @@ import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.xpath.*;
 import java.io.File;
 import java.io.IOException;
+import java.util.List;
 
 /**
  * com.huangluyu.bdmorm.bootstrap.scan
@@ -23,35 +26,31 @@ public class XmlRegister implements BaseFileRegister {
 
     private static final String DATA_SOURCE = "/dataSource";
 
-    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";
+    private static final String CONNECTION = "/connection";
+
+    private static final String DB_TYPE = "/@type";
+    private static final String DB_NAME = "/name";
+    private static final String DB_URL = "/url";
+    private static final String DB_USER = "/user";
+    private static final String DB_PASSWORD = "/password";
+
+    private static final String XML_FILE_NAME = "bdm.xml";
 
     @Override
     public void handleFile(File file) {
-        if (file.getName().equals("bdm.xml")) {
+        if (file.getName().equals(XML_FILE_NAME) && BdmWorkSpace.dataBaseInfoList.isEmpty()) {
             try {
                 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);
-                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) {
-                e.printStackTrace();
-            } catch (SAXException e) {
-                e.printStackTrace();
-            } catch (XPathExpressionException e) {
+                // TODO 优化XML读取
+                NodeList connections = (NodeList) xpath.compile(DATA_SOURCE + CONNECTION).evaluate(doc, XPathConstants.NODESET);
+                for (int i = 0; i < connections.getLength(); i++) {
+                    Node connection = connections.item(i);
+                    DataBaseInfo dataBaseInfo = getDataBaseInfo(xpath, connection);
+                    BdmWorkSpace.dataBaseInfoList.add(dataBaseInfo);
+                }
+            } catch (ParserConfigurationException | IOException | SAXException | XPathExpressionException e) {
                 e.printStackTrace();
             }
         }
@@ -64,8 +63,18 @@ public class XmlRegister implements BaseFileRegister {
 
     private Document getDocument(String filePath) throws ParserConfigurationException, IOException, SAXException {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-        factory.setNamespaceAware(true); // never forget this!
+        factory.setNamespaceAware(true);
         DocumentBuilder builder = factory.newDocumentBuilder();
         return builder.parse(filePath);
     }
+
+    private DataBaseInfo getDataBaseInfo(XPath xpath, Node connection) throws XPathExpressionException {
+        String dbName = (String) xpath.compile("./" + DB_NAME).evaluate(connection, XPathConstants.STRING);
+        String dbUrl = (String) xpath.compile("./" + DB_URL).evaluate(connection, XPathConstants.STRING);
+        String dbUser = (String) xpath.compile("./" + DB_USER).evaluate(connection, XPathConstants.STRING);
+        String dbPassword = (String) xpath.compile("./" + DB_PASSWORD).evaluate(connection, XPathConstants.STRING);
+        String dbType = (String) xpath.compile("./" + DB_TYPE).evaluate(connection, XPathConstants.STRING);
+        DataBaseType dataBaseType = DataBaseType.valueOf(dbType.toUpperCase());
+        return new DataBaseInfo(dbName, dbUrl, dbUser, dbPassword, dataBaseType);
+    }
 }

+ 25 - 11
src/com/huangluyu/bdmorm/constructor/enums/ConditionType.java

@@ -42,7 +42,17 @@ public enum ConditionType {
     /**
      * 小于等于
      */
-    EQUALS_LESS_THAN;
+    EQUALS_LESS_THAN,
+
+    /**
+     * 通配包含
+     */
+    LIKE,
+
+    /**
+     * 通配排除
+     */
+    NOT_LIKE;
 
     @Override
     public String toString() {
@@ -50,7 +60,7 @@ public enum ConditionType {
             case EQUALS:
                 return "=";
             case IN:
-                return "in";
+                return "IN";
             case LESS_THAN:
                 return "<";
             case GREATER_THAN:
@@ -61,6 +71,10 @@ public enum ConditionType {
                 return ">=";
             case EQUALS_LESS_THAN:
                 return "<=";
+            case LIKE:
+                return "LIKE";
+            case NOT_LIKE:
+                return "NOT_LIKE";
             default:
         }
         return super.toString();
@@ -79,21 +93,21 @@ public enum ConditionType {
     public static ConditionType valueOfSafe(String value) {
         value = value.toUpperCase();
         switch (value) {
-            case "EQUALS_GREATER_THAN" : return ConditionType.EQUALS_GREATER_THAN;
             case ">=" : return ConditionType.EQUALS_GREATER_THAN;
-            case "EQUALS_LESS_THAN" : return ConditionType.EQUALS_LESS_THAN;
             case "<=" : return ConditionType.EQUALS_LESS_THAN;
-            case "GREATER_THAN" : return ConditionType.GREATER_THAN;
             case ">" : return ConditionType.GREATER_THAN;
-            case "NOT_EQUALS" : return ConditionType.NOT_EQUALS;
             case "!=" : return ConditionType.NOT_EQUALS;
-            case "LESS_THAN" : return ConditionType.LESS_THAN;
             case "<" : return ConditionType.LESS_THAN;
-            case "EQUALS" : return ConditionType.EQUALS;
             case "=" : return ConditionType.EQUALS;
-            case "IN" : return ConditionType.IN;
-            case "in" : return ConditionType.IN;
-            default: return null;
+            case "like" : return ConditionType.LIKE;
+            case "not like" : return ConditionType.NOT_LIKE;
+            default: {
+                try {
+                    return ConditionType.valueOf(value);
+                } catch (Exception e) {
+                    return null;
+                }
+            }
         }
     }
 

+ 3 - 3
src/com/huangluyu/test/PropertiesTest.java

@@ -12,12 +12,12 @@ public class PropertiesTest {
 
     @Test
     public void test() throws SQLException {
-        List<BaseModel> person2 = Table.from(Person.class)
+        List<BaseModel> person = Table.from(Person.class)
                 .leftJoin("person_info pi", "person.id", "pi.id")
-                .where("person.name", "=", "xiaoMing")
+                .where("person.name", "like", "xiaoMing%")
                 .orderBy("person.name", "desc")
                 .select("*")
                 .get();
-        System.out.println(person2);
+        System.out.println(person);
     }
 }

+ 9 - 7
src/com/huangluyu/test/configure/bdm.xml

@@ -3,15 +3,17 @@
 <dataSource>
 
     <connection type="mysql">
-        <url>jdbc:mysql://localhost:3306/bdm_orm</url>
-        <user>bdm_orm</user>
-        <password>123456</password>
+        <name>数据库1</name>
+        <url>jdbc:mysql://huangluyu.com:3306/bdm_orm</url>
+        <user>test</user>
+        <password>abc123!@#ABC</password>
     </connection>
 
-    <connection type="oracle">
-        <url>jdbc:oracle://localhost:1521/bdm_orm</url>
-        <user>bdm_orm</user>
-        <password>123456</password>
+    <connection type="mysql">
+        <name>数据库2</name>
+        <url>jdbc:mysql://huangluyu.com:3306/bdm_orm2</url>
+        <user>test</user>
+        <password>abc123!@#ABC</password>
     </connection>
 
 </dataSource>