![](/img/trans.png)
[英]How to read database configuration parameter using properties file in hibernate
[英]Read the Properties in hibernate Configuration File
由於某種原因,我希望能夠讀取休眠配置文件中的屬性,例如說我需要知道使用了哪種方言或哪種數據庫驅動程序類。 我試圖通過解析xml來做到這一點,但是我並沒有越過這些屬性:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
*
* @author User
*/
public class XmlParser {
public XmlParser() {
}
public void readXml() throws SAXException, IOException, ParserConfigurationException {
InputStream in = this.getClass().getResourceAsStream("/hibernate.cfg.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(in);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("property");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Driver : " + eElement.getAttribute("name"));
System.out.println("Not Sure : " + eElement.getElementsByTagName("hibernate.connection.url").item(0).getTextContent());
System.out.println(eElement.getElementsByTagNameNS("name", "hibernate.dialect").item(0).getTextContent());
}
}
}
}
我的輸出是:
Root element :hibernate-configuration
----------------------------
Current Element :property
Driver : hibernate.dialect
null
我需要能夠隨時在應用程序中獲取屬性值。 還有其他方法或者我在此方法中缺少什么嗎?
你可以嘗試一下嗎
Configuration hibernateConfiguration = new Configuration().configure(new File("/hibernate.cfg.xml"));
String url = configuration.getProperty("hibernate.connection.url");
這樣,您可以獲取所有屬性,在靜態hashMap中的某個位置進行設置,然后在整個應用程序中使用。
您可以使用SAXReader來解析文件,這是代碼:
import java.io.InputStream;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ReadXMLFile {
public static void main(String argv[]) throws DocumentException {
new ReadXMLFile().readMyXML();
}
private void readMyXML() throws DocumentException {
InputStream in = this.getClass().getResourceAsStream(
"/hibernate.cfg.xml");
Document document = new SAXReader().read(in);
Element root = document.getRootElement();
System.out.println("Root element : " + root.getName());
Element sfNode = document.getRootElement().element("session-factory");
Iterator<Element> itr = sfNode.elementIterator("property");
System.out.println("----------Properties-----------");
while (itr.hasNext()) {
Element node = itr.next();
String name = node.attributeValue("name");
String value = node.getText().trim();
System.out.println(name + " = " + value);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.