从 war maven 外部化 hibernate.cfg.xml
Externalize hibernate.cfg.xml from war maven
我正在将我的 java Web 应用程序打包到一个包含所有依赖项和其他内容的 zip 文件中......使用 maven 程序集插件,我编写了一个批处理脚本以部署 war并且 运行 它没有打开 eclipse ...
这里的问题是我想 运行 这个批处理文件用于其他计算机上的其他数据库,在这里我需要你的帮助来告诉我是否有办法将 hibernate.cfg.xml 外化到直接配置我的应用程序将链接到的数据库。
提前致谢
您需要配置 property
文件以外部化 hibernate.cfg.xml
。在 class 路径中创建 hibernate.properties
并为休眠配置设置变量和值。您可以参考下面的示例代码:
hibernate.cfg.xml:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<map>
<entry key="connection.driver_class" value="${hibernate.connection.driver_class}" />
<entry key="connection.username" value="${hibernate.connection.username}" />
<entry key="connection.password" value="${hibernate.connection.password}" />
<entry key="transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
</map>
<property>
</bean>
hibernate.properties:
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test
hibernate.connection.username = root
hibernate.connection.password = root
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.current_session_context_class=thread
我发现我必须在 hibernate.properties
中指定连接属性(用户名、密码、connection.url、方言...)。
class 映射必须保留在 hibernate.cfg.xml
文件中。
通过将它们都放在 resources
,Hibernate 将找到它们并提取他需要的东西 :D
我正在将我的 java Web 应用程序打包到一个包含所有依赖项和其他内容的 zip 文件中......使用 maven 程序集插件,我编写了一个批处理脚本以部署 war并且 运行 它没有打开 eclipse ...
这里的问题是我想 运行 这个批处理文件用于其他计算机上的其他数据库,在这里我需要你的帮助来告诉我是否有办法将 hibernate.cfg.xml 外化到直接配置我的应用程序将链接到的数据库。
提前致谢
您需要配置 property
文件以外部化 hibernate.cfg.xml
。在 class 路径中创建 hibernate.properties
并为休眠配置设置变量和值。您可以参考下面的示例代码:
hibernate.cfg.xml:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<map>
<entry key="connection.driver_class" value="${hibernate.connection.driver_class}" />
<entry key="connection.username" value="${hibernate.connection.username}" />
<entry key="connection.password" value="${hibernate.connection.password}" />
<entry key="transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
</map>
<property>
</bean>
hibernate.properties:
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test
hibernate.connection.username = root
hibernate.connection.password = root
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.current_session_context_class=thread
我发现我必须在 hibernate.properties
中指定连接属性(用户名、密码、connection.url、方言...)。
class 映射必须保留在 hibernate.cfg.xml
文件中。
通过将它们都放在 resources
,Hibernate 将找到它们并提取他需要的东西 :D