package com.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Create {
public static void main(String[] args) {
//document
Document document = DocumentHelper.createDocument();
//设置DocType
document.addDocType("hibernate-configuration" , "-//Hibernate/Hibernate Configuration DTD 3.0//EN" , "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
//hibernate-configuration
Element configuration = document.addElement("hibernate-configuration");
//session-factory
Element sessionfactory = configuration.addElement("session-factory");
sessionfactory.addComment("Database connection settings");
//driver
Element classDriver = sessionfactory.addElement("property");
classDriver.addAttribute("name", "connection.driver_class");
classDriver.setText("com.mysql.jdbc.Driver");
//url
Element url = sessionfactory.addElement("property");
url.addAttribute("name", "connection.url");
url.setText("jdbc:mysql://localhost:3306/1ting");
//username
Element username = sessionfactory.addElement("property");
username.addAttribute("name", "connection.username");
username.setText("root");
//password
Element password = sessionfactory.addElement("property");
password.addAttribute("name", "connection.password");
password.setText("root");
//pool
sessionfactory.addComment("pool size management");
Element pool = sessionfactory.addElement("property");
pool.addAttribute("name", "connection.pool_size");
pool.setText("1");
//dialect
sessionfactory.addComment("dialect management");
Element dialect = sessionfactory.addElement("property");
dialect.addAttribute("name", "dialect");
dialect.setText("org.hibernate.dialect.MySQL5Dialect");
//showsql
sessionfactory.addComment("showsql");
Element showsql = sessionfactory.addElement("property");
showsql.addAttribute("name", "show_sql");
showsql.setText("true");
//hbm2ddl
sessionfactory.addComment("Drop and re-create the database schema on startup");
Element hbm2ddl = sessionfactory.addElement("property");
hbm2ddl.addAttribute("name", "hbm2ddl.auto");
hbm2ddl.setText("update");
//mapping
sessionfactory.addComment("mapping class");
Element mapping1 = sessionfactory.addElement("mapping");
mapping1.addAttribute("class", "edu.hzu.entity.SingerType");
Element mapping2 = sessionfactory.addElement("mapping");
mapping2.addAttribute("class", "edu.hzu.entity.Singer");
//生成xml
XMLWriter writer = null;
//设置输出格式
OutputFormat format = new OutputFormat();
format.setEncoding("utf-8");
format.setIndent(true);
format.setLineSeparator("\n");
format.setNewlines(true);
try {
writer = new XMLWriter(format);
writer.setOutputStream(new FileOutputStream("hibernate.cfg.xml"));
writer.write(document);
writer.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.test;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
public class Reader {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
SAXReader reader = new SAXReader();
System.out.println("read all properties");
System.out.println("-------------------");
try {
Document document = reader.read(new File("hibernate.cfg.xml"));
DefaultXPath propertyPath = new DefaultXPath("/hibernate-configuration/session-factory/property");
List<Element> selectNodes = propertyPath.selectNodes(document);
for (Element property : selectNodes) {
String name = property.attributeValue("name");
String text = property.getText();
System.out.println(name + " : " + text);
}
System.out.println("-------------------");
System.out.println("Mappings");
System.out.println("-------------------");
DefaultXPath mappingPath = new DefaultXPath("/hibernate-configuration/session-factory/mapping");
List<Element> mappings = mappingPath.selectNodes(document);
for (Element mapping : mappings) {
System.out.println(mapping.attributeValue("class"));
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}