最近一直想寫一個mybatis 相似的ORM框架。先從模仿mybatis開始。java
先從解析mybatis配置文件開始(簡化版解析部分屬性,後面會完善的)node
下面是mybatis的配置文件數據庫
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 配置文件的根元素 --> <configuration> <!-- 屬性:定義配置外在化 --> <properties></properties> <!-- 設置:定義mybatis的一些全局性設置 --> <settings> <!-- 具體的參數名和參數值 --> <setting name="" value=""/> </settings> <!-- 類型名稱:爲一些類定義別名 --> <typeAliases></typeAliases> <!-- 類型處理器:定義Java類型與數據庫中的數據類型之間的轉換關係 --> <typeHandlers></typeHandlers> <!-- 對象工廠 --> <objectFactory type=""></objectFactory> <!-- 插件:mybatis的插件,插件能夠修改mybatis的內部運行規則 --> <plugins> <plugin interceptor=""></plugin> </plugins> <!-- 環境:配置mybatis的環境 --> <environments default=""> <!-- 環境變量:能夠配置多個環境變量,好比使用多數據源時,就須要配置多個環境變量 --> <environment id=""> <!-- 事務管理器 --> <transactionManager type=""></transactionManager> <!-- 數據源 --> <dataSource type="POOLED"> <property name="driver" value="driver"/> <property name="url" value="url"/> <property name="username" value="username"/> <property name="password" value="password"/> </dataSource> </environment> </environments> <!-- 數據庫廠商標識 --> <databaseIdProvider type=""></databaseIdProvider> <!-- 映射器:指定映射文件或者映射類 --> <mappers></mappers> </configuration>
先從解析dataSource開始mybatis
package xml; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.junit.Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * Created by dingshuangkun on 2017/12/2. */ public class XMLSingleton { private final String DATASOUCE="dataSource"; private final String DRIVER="driver"; private final String URL="url"; private final String USERNAME="username"; private final String PASSWORD="password"; private final String PROPERTY="property"; private static Document document; public Document getDocument() { return document; } private static XMLSingleton xmlSingleton = null; //單例模式 private XMLSingleton(){} public static XMLSingleton getInstance(){ if(xmlSingleton == null){ synchronized(XMLSingleton.class){ if(xmlSingleton == null){ return new XMLSingleton(); } } } return xmlSingleton; } //加載mybatis配置文件 public Document load(String path)throws Exception{ SAXReader reader = new SAXReader(); document= reader.read(new File(path)); return document; } public Map<String,String> xmlParse(Document document){ try { //先拿到根節點 Element root=document.getRootElement(); Map<String,String> nodeMap=new HashMap<>(); //解析根節點 Map<String,String>map= listNodes(root,nodeMap); return map; }catch (Exception e){ e.printStackTrace(); } return null; } public Map<String,String> listNodes(Element node,Map<String,String>map){ if(DATASOUCE.equals(node.getName())) { // 取出節點對應的屬性(type->POOLED) <dataSource type="POOLED"> List<Attribute> list= node.attributes(); for(Attribute a:list){ map.put(a.getName(),a.getValue()); } // 取出子元素<property name="driver" value="driver"/> Iterator<Element> it=node.elementIterator(); // 獲取 name->value 存入map中 while (it.hasNext()){ Element element= it.next(); if(PROPERTY.equals(element.getName())){ List<Attribute> attributes=element.attributes(); Iterator<Attribute> ita=attributes.iterator(); map.put(ita.next().getValue(),ita.next().getValue()); } } } // 遞歸葉子節點 Iterator<Element> iterator = node.elementIterator(); while(iterator.hasNext()){ Element e = iterator.next(); listNodes(e,map); } return map; } }