我在我的应用程序中同时使用了 Liquibase
和 Hibernate
库。它们都使用同一个数据库,但每个都需要自己的 properties
文件。但是,一些特定于数据库的字段是常见的。我想避免重复这些字段并将它们放在两个文件中。我希望将这些属性放在一个文件中,Liquibase 和 Hibernate 属性文件将从中读取。
目前我有。
liquibase.properties
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root
hibernate .cfg.xml
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
请注意驱动程序、URL、用户名和密码在这 2 个文件中是如何重复的。理想情况下,我会有这样的第三个文件。
database.properties
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root
然后是类似的东西。
liquibase.properties
driver: ${driver}
url: ${url}
username: ${username}
password: ${password}
hibernate .cfg.xml
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<!-- Database connection settings -->
<property name="connection.driver_class">${driver}</property>
<property name="connection.url">${url}</property>
<property name="connection.username">${username}</property>
<property name="connection.password">${password}</property>
这样的事情可能吗?
最佳答案
这是一个 SO 答案(和问题),显示了如何将 hibernate 数据库配置放入属性文件中:https://stackoverflow.com/a/25685198/332248
这里是关于 liquibase 如何为 liquibase 设置属性文件的文档(使用 --defaultsFile
选项):http://www.liquibase.org/documentation/command_line.html
因此应该可以将所有内容合并到一个 hibernate.properties 文件中,然后在运行 liquibase 时指定同一文件。
关于java - Liquibase 和 Hibernate 从相同的属性文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48336977/