2014-09-05 36 views
1

在我的应用程序中,我使用hibernate连接数据库并创建会话。这是我的hibernate.cfg.xml文件。还行吧。它工作正常。如何在休眠状态下使用属性文件读取数据库配置参数

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/country</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">password</property> 

    </session-factory> 

</hibernate-configuration> 

但是当我尝试读取使用此hibernate.cfg.xml使用db.property file数据库配置属性,它显示异常,这是我的另一个hibernate.cfg.xml文件

<util:properties id="db" location="classpath:db.properties" /> 

<hibernate-configuration> 

    <session-factory> 
     <!-- Database connection settings --> 
     <property name="driverClassName" value="#{db['driverClassName']}"></property> 
     <property name="url" value="#{db['url']}"></property> 
     <property name="username" value="#{db['username']}"></property> 
     <property name="password" value="#{db['password']}"></property> 

    </session-factory> 

</hibernate-configuration> 

这是错误

org.dom4j.DocumentException: Error on line 8 of document : The prefix "util" for  element "util:properties" is not bound. Nested exception: The prefix "util" for element "util:properties" is not bound. 
    at org.dom4j.io.SAXReader.read(SAXReader.java:482) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155) 

这是我的属性文件名为db.properties

driverClassName=com.mysql.jdbc.Driver 

url=jdbc:mysql://localhost:3306/country 

username=root 

password=password 

有什么问题? 如何正确执行此操作

+0

如果您使用的春天,那么你可以参考这个链接,HTTP://stackoverflow.com/questions/17939339/propertyplaceholderconfigurer-with-hibernate-cfg-xml – 2014-09-05 11:37:43

+0

我读到,但我只需要在休眠状态下执行,而不是使用spring。因为我希望我的hibernate应用程序从属性文件中获取数据库配置属性。 – Killer 2014-09-05 11:39:43

+0

@Killer does [this](http://stackoverflow.com/questions/24176024/how-to-include-properties-from-external-file-to-hibernate-cfg-xml)帮助你 – 2014-09-05 11:42:05

回答

6

util:properties不是在hibernate.cfg.xml文件中使用的有效标记。如果要将所有数据库配置详细信息放在属性文件中,则可以将它们放在hibernate.properties文件中,并从hibernate.cfg.xml文件中删除这些文件。通过这种方式,数据库详细信息将保存在属性文件中。

如果你想保持,而不是使用hibernate.properties文件,那么你可以试试这个单独的文件:

java.util.Properties properties = new Properties(); 
properties.load(new FileInputStream("db.properties")); 

Configuration configuration = new Configuration(); 

configuration.configure("hibernate.cfg.xml").addProperties(properties);; 

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() 
.applySettings(configuration.getProperties()).build(); 

SessionFactory sessionFactory = configuration 
.buildSessionFactory(serviceRegistry); 
+0

这个代码是什么文件进去? – buddamus 2016-05-06 17:34:46

+0

想通了:它在哪个文件正在调用.buildSessionFactory().. – buddamus 2016-05-06 19:25:06

4

试试这个代码:

hibernate.properties

hibernate.connection.url=jdbc:mysql://localhost:3306/country 
hibernate.connection.driver_class=com.mysql.jdbc.Driver 
hibernate.connection.username=root 
hibernate.connection.password=123 

HibernateUtil.java

import java.util.Properties; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 

public class HibernateUtil { 

private static final SessionFactory sessionFactory = buildSessionFactory(); 

private static SessionFactory buildSessionFactory() { 
    try { 

     Properties dbConnectionProperties = new Properties(); 
     try { 
      dbConnectionProperties.load(HibernateUtil.class.getClassLoader().getSystemClassLoader().getResourceAsStream("hibernate.properties")); 
     } catch(Exception e) { 
      e.printStackTrace(); 
      // Log 
     }   

     return new AnnotationConfiguration().mergeProperties(dbConnectionProperties).configure("hibernate.cfg.xml").buildSessionFactory();   


    } catch (Throwable ex) { 
     ex.printStackTrace(); 
//   throw new ExceptionInInitializerError(ex); 
    } 
    return null; 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

} 

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
<session-factory> 

    <!-- Database connection settings --> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

    <!-- Enable Hibernate's automatic session context management --> 
    <property name="current_session_context_class">thread</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">false</property> 

    <property name="hibernate.hbm2ddl.auto">update</property> 
    <!--<mapping class="net.viralpatel.hibernate.Employee"/>--> 
    <!--<mapping class="net.viralpatel.hibernate.PersonEntity"/>--> 
    <mapping class="mobin.FavaEmail.entities.PersonEntity"/> 
    <mapping class="mobin.FavaEmail.entities.OrgEntity"/> 
    <mapping class="mobin.FavaEmail.entities.User"/> 


</session-factory> 
</hibernate-configuration> 
+1

“HibernateUtil.java”将在我的项目中?我不清楚我的项目如何知道使用这个文件... – buddamus 2016-05-06 17:31:51

+0

使用你想创建seassionFactory的地方。 SessionFactory factory = HibernateUtil.getSessionFactory(); – hossein 2016-05-09 19:54:44

相关问题