用户登录
用户注册

分享至

修改hibernate.cfg.xml

  • 作者: _____弹情丶
  • 来源: 51数据库
  • 2021-01-23
建议你用jdom类库,那个才是专为java设计的
public class TestHibernateHome {
public static void main(String[] args) throws Exception {
String fileName = "E:/hibernate.cfg.xml";//路径
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(fileName);
Element el = doc.getRootElement();
List<Element> list = el.getChildren("session-factory");
for (Element e : list) {

List<Element> listTemp = e.getChildren("property");
for (Element ee : listTemp) {
String pro = ee.getValue();
System.out.println("值: "+pro);
String name1 = ee.getAttributeValue("name");
System.out.println("属性 : " + name1);
}

List<Element> listTemp1 = e.getChildren("mapping");
for (Element ee : listTemp1) {
String map = ee.getValue();
System.out.println("值: "+map);
String name2 = ee.getAttributeValue("resource");
System.out.println("属性 : " + name2);
}

}
}
}



  hibernate默认的配置文件路径是/hibernate.cfg.xml,如果你不想使用默认路径。比如你把配置文件都放到conf目录下面。那如何修改hibernate的config路径呢。其实也很简单。看我写的hibernateuti类。
(hibernate hibernate.cfg.xml 配置路径) (hibernate hibernate.cfg.xml 配置路径)
(hibernate hibernate.cfg.xml 配置路径)(hibernate hibernate.cfg.xml 配置路径)
(hibernate hibernate.cfg.xml 配置路径)(hibernate hibernate.cfg.xml 配置路径)
我们只要把
sessionfactory = new annotationconfiguration().configure().buildsessionfactory();
改为:
sessionfactory = new annotationconfiguration().configure(new file("../conf/hibernate.cfg.xml")).buildsessionfactory();
就可也了。
../conf/hibernate.cfg.xml就是你的路径。



  这个在myeclipse里有工具



  不建议。



  参数是样式表加路径名
public void test(String FilePathName) {
InputStream ins = null;
try {
ins = new FileInputStream(FilePathName);

} catch (Exception e) {
e.printStackTrace();
}
Document doc = null;
DOMBuilder domB = new DOMBuilder();
try {
doc = domB.build(ins);
} catch (JDOMException ex) {
ex.printStackTrace();
}
String value = doc.getRootElement().getChildText("节点名称");
System.out.println("节点名称对应的值:" + value);
}



  建议用myeclipse开发工具,导入进去,改一下就ok了~~
软件
前端设计
程序设计
Java相关