gpt4 book ai didi

java - 如何在数据库属性文件中获取用户名和密码的值而不是 hibernate.cfg.xml

转载 作者:行者123 更新时间:2023-11-29 13:55:37 25 4
gpt4 key购买 nike

您好,我是 spring 和 hibernate 的新手,我想使用 database.properties 文件中的用户名和密码值,而不是 hibernate.cfg.xml 中的值。我不知道也许我在某处做错了。在我的 hibernate.cfg.xml 文件中,我注释掉了用户名和密码,因为它在我提供时有效。我还将我的 database.properties 文件放在与我的项目不同的位置

database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/mzanzi-fm
database.user=postgres
database.password=1234
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

这是我的 hibernate.cfg.xml 文件

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mzanzi-fm</property>
<property name="show_sql">true</property>
<property name= "hbm2ddl.auto">update</property>

<mapping class="com.mzanzi.admin.model.User"/>
<mapping class="com.mzanzi.admin.model.Genres"/>
<mapping class="com.mzanzi.admin.model.Album"/>
<mapping class="com.mzanzi.admin.model.Artists"/>
<mapping class="com.mzanzi.admin.model.Songs"/>
<mapping class="com.mzanzi.admin.model.Usergroups"/>


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

这是我的上下文文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<context:property-placeholder location="file:C:\Users\Tumi Koma\Documents\resources\database.properties" />
<context:component-scan base-package="com.mzanzi.admin" />

<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

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

</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="annotatedClasses">
<list>
<value>com.mzanzi.admin.model.User</value>
<value>com.mzanzi.admin.model.Genres</value>
<value>com.mzanzi.admin.model.Album</value>
<value>com.mzanzi.admin.model.Artists</value>
<value>com.mzanzi.admin.model.Songs</value>
<value>com.mzanzi.admin.model.Usergroups</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

</bean>

<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

非常感谢任何帮助,谢谢

最佳答案

您可以使用属性占位符来实现这一点。这是您的上下文文件的样子

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${app.jdbc.driverClassName}"
p:url="${app.jdbc.url}" p:username="${app.jdbc.username}" p:password="${app.jdbc.password}"
p:validationQuery="SELECT 1" />

这是您的 datasource.properties 文件

app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost:3306/testdb
app.jdbc.username=root
app.jdbc.password=password

要实现这一点,请将其添加到您的配置文件中(假设您在类路径中有该文件)

        <context:property-placeholder
location="classpath:datasource.properties,classpath:mailsender.properties,classpath:repository.properties" />

datasource.properties 文件对应于您的 database.properties 文件。

this帖子可能会对您有所帮助。

关于java - 如何在数据库属性文件中获取用户名和密码的值而不是 hibernate.cfg.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32716318/

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