如何在hibernate中使用属性文件读取数据库配置参数 [英] How to read database configuration parameter using properties file in hibernate
问题描述
在我的应用程序中,我使用hibernate连接数据库并创建会话。这是我的hibernate.cfg.xml文件。还行吧。它正常工作。
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-configuration -3.0.dtd>
< hibernate-configuration>
< session-factory>
<! - 数据库连接设置 - >
< 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>密码< / property>
< / session-factory>
< / hibernate-configuration>
但是当我尝试使用 db.property文件读取数据库配置属性时
使用这个 hibernate.cfg.xml
,它显示Exception,这是我的另一个 hibernate.cfg.xml
file
< util:properties id =dblocation =classpath:db.properties/>
< hibernate-configuration>
< session-factory>
<! - 数据库连接设置 - >
< property name =urlvalue =#{db ['url']}>< / property>
< property name =usernamevalue =#{db ['username']}>< / property>
< property name =passwordvalue =#{db ['password']}>< / property>
< / session-factory>
< / hibernate-configuration>
这是错误
org.dom4j.DocumentException:文档第8行的错误:元素util:properties的前缀util未绑定。嵌套异常:未绑定元素util:properties的前缀util。
>
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 =密码
什么是问题在那里?
如何正确地做到这一点
解决方案
util:properties
不是在hibernate.cfg.xml
文件中使用的有效标记。如果你想把所有的数据库配置细节放在一个属性文件中,那么你可以把它们放在hibernate.properties
文件中,并从hibernate.cfg .xml
文件。以这种方式,DB详细信息将保存在属性文件中。
如果您想要维护一个单独的文件而不是使用hibernate.properties文件,那么您可以试试这个:
java.util.Properties properties = new Properties();
properties.load(new FileInputStream(db.properties));
配置配置=新配置();
configuration.configure(hibernate.cfg.xml)。addProperties(properties);;
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())。build();
SessionFactory sessionFactory =配置
.buildSessionFactory(serviceRegistry);
In my apps I am using hibernate, to connect a with database and create a session. this is my hibernate.cfg.xml file. This is ok. It working properly.
<?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>
But when I try to read the DB configuration properties using
db.property file
using thishibernate.cfg.xml
, it showing Exception, this is my anotherhibernate.cfg.xml
file
<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>
this is the error
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)
this is my properties file named
db.properties
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/country username=root password=password
what is the problem is there? how to do that properly
解决方案
util:properties
is not a valid tag to use inhibernate.cfg.xml
file. If you want to place all the DB configuration details in a properties file then you can place them inhibernate.properties
file and remove those fromhibernate.cfg.xml
file. In this way the DB details will be maintained in properties file.If you want to maintain a separate file instead of using hibernate.properties file then you can try this:
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);
这篇关于如何在hibernate中使用属性文件读取数据库配置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!