【问题标题】:Persistence.xml field values from properties file属性文件中的 Persistence.xml 字段值
【发布时间】:2013-12-27 11:03:02
【问题描述】:

求助,我想将我的持久化 xml 属性值引用到我的 db.properties 文件中。

这是我的 db.properties 文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/apsas
jdbc.username=root
jdbc.password=password

这是我当前的 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.hibernate.ejb.HibernatePersistence
        </provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/apsas" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

我想做的就是把它的属性设置成这样

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.hibernate.ejb.HibernatePersistence
        </provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="${jdbc.driver}" />
            <property name="hibernate.connection.url" value="${jdbc.url}" />
            <property name="hibernate.connection.username" value="${jdbc.username}" />
            <property name="hibernate.connection.password" value="${jdbc.password}" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

问题是如何将属性文件导入我的持久性 xml 提前感谢那些帮助我的人......

【问题讨论】:

  • 你在使用 maven 吗?

标签: java hibernate jpa properties persistence.xml


【解决方案1】:

如果您使用的是 maven,则可以为此使用源过滤。使用this docs 作为参考。我将简要介绍一下过程。

您需要指定过滤路径。我不确定您是否必须明确定义包含的文件。

<build>
  ...
  <resources>
    <resource>
      <directory>src/main/resources/META-INF</directory>
      <filtering>true</filtering>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
    ...
  </resources>
  ...
</build>

你定义的目录是相对于pom.xml的。

您现在可以定义常规 maven 属性来替换 persistence.xml 中的占位符

如果您想将属性放在单独的 .properties 文件中,您需要告诉 maven,在哪里可以找到该文件:

<filters>
  <filter>db.properties</filter>
</filters>

过滤发生在mvn resources:resources。此步骤是为您将在部署时执行的所有打包目标定义的。

您可以使用 Maven 配置文件在属性集或 .properties 文件之间切换。

【讨论】:

猜你喜欢
  • 2015-05-17
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
相关资源
最近更新 更多