Hibernate有两种配置文件格式一种是XML默认为hibernatecfgxml一种是properties默认为hibernateproperties不同的配置文件Hibernate有两种配置文件格式一种是XML默认为hibernatecfgxml一种是properties默认为hibernateproperties
不同的配置文件对Hibernate的初始化方法是不一样的
比如如果采用properties文件作为配置文件
那么初始化的代码大致为
Configuration config = new Configuration();
configaddClass(myclassclass);
如果配置文件为XML则
Configuration config = new Configuration(nfig();
XML文件格式的配置文件不支持addClass方法!!!这是因为在配置文件XML文件中已经定义了Mpaaing文件因此就不需要在用编码方式导入POJO文件了
另网上好多文章甚至有的书都说Hibenate的配置文件必须放在class的根目录参考一下API发现这个说法是不正确的例如Configuration config = new Configuration()config(配置文件名)完全可以的
所有config方法如下
addCacheableFile(File xmlFile)
If a cached xmlFile + bin exists and is newer than xmlFile the bin file will be read directly
Configuration addClass(Class persistentClass)
Read a mapping from an application resource using a convention
Configuration addDirectory(File dir)
Read all mapping documents from a directory tree
Configuration addDocument(orgwcdomDocument doc)
Read mappings from a DOM Document
Configuration addFile(File xmlFile)
Read mappings from a particular XML file
Configuration addFile(String xmlFile)
Read mappings from a particular XML file
void addFilterDefinition(FilterDefinition definition)
Configuration addInputStream(InputStream xmlInputStream)
Read mappings from an InputStream
Configuration addJar(File jar)
Read all mappings from a jar file
Configuration addProperties(Properties extraProperties)
Set the given properties
Configuration addResource(String path)
Read mappings from an application resource trying different classloaders
Configuration addResource(String path ClassLoader classLoader)
Read mappings from an application resource
Configuration addURL(URL url)
Read mappings from a URL
Configuration addXML(String xml)
Read mappings from a String