gpt4 book ai didi

hibernate - 如何换出 hibernate.cfg.xml 属性?

转载 作者:行者123 更新时间:2023-12-04 12:00:44 25 4
gpt4 key购买 nike

我有一个 Hibernate 配置文件 hibernate.cfg.xml硬编码的属性名称在哪里,例如:

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysecretpassword</property>
...
</session-factory>
</hibernate-configuration>

我想把用户名和密码之类的东西换成 .properties -文件。这样我就会得到以下信息:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">${jdbc.username}</property>
<property name="hibernate.connection.password">${jdbc.password}</property>
...
</session-factory>
</hibernate-configuration>

我怎样才能做到这一点?对于 Spring 中的数据源 我可以在我的 applicationContext.xml 中使用这个:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

Hibernate 的等价物是什么?

最佳答案

从 hibernate.cfg.xml 中删除配置参数并声明以下内容:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
</value>
</property>
</bean>

关于hibernate - 如何换出 hibernate.cfg.xml 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3795279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com