gpt4 book ai didi

spring - 如何使用 liquibase 在 spring 中从数据库和持久性实体之间的差异生成变更日志?

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

我需要在 spring(基于 JAVA 的配置)而不是 spring boot 中解决上述问题。我在网上找到的大部分教程都是基于 spring boot 但我的应用程序没有在 spring boot 上运行,只是 spring mvc。我的类路径中已经有必要的依赖项,即 liquibase-core-3.5.3 和 liquibase-hibernate5-3.6 jar。

我还创建了 liquibase 属性文件(也在类路径中),如下所示

driver=com.mysql.jdbc.Driver
changeLogFile=classpath:db-changelog.xml
url=jdbc:mysql://localhost:3306/mydatabase
username=myusername
password=mypassword
referenceUrl=hibernate:spring:com.mypackage.entity?dialect=org.hibernate.dialect.MySQL5InnoDBDialect
diffChangeLogFile=com/mypackage/dbchangelog/diff-changelog.xml

现在的问题是我如何执行它来生成差异更新日志文件?

最佳答案

您必须使用 liquibase 插件来完成此操作,具体取决于您用于构建的内容。

Maven plugin 是关于如何运行 liquibase 命令的非常好的文档。

如果您使用的是 gradle,liquibase 插件在调用 diffChangeLog 时无法正常工作。

基于 this answer ,创建一个名为 liquibase.gradle 的文件。我已经根据 hibernate5 编辑了一些属性,因为它不再支持 ImprovedNamingStrategy

def diffLog = "$projectDir/src/main/resources/db/changelog/db.changelog.yaml"

configurations {
liquibase
}

dependencies {
liquibase 'org.liquibase.ext:liquibase-hibernate5:3.6'
}

task liquibaseDiffChangelog(type: JavaExec) {
group = "liquibase"

classpath sourceSets.main.runtimeClasspath
classpath configurations.liquibase
main = "liquibase.integration.commandline.Main"

args "--changeLogFile=" + diffLog
args "--referenceUrl=hibernate:spring:com.mypackage.entity?hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&dialect=org.hibernate.dialect.MySQL5Dialect"
args "--username=<username>"
args "--password=<password>"
args "--url=jdbc:mysql://<db_host_and_port>/<db_name>"
args "--referenceDriver=liquibase.ext.hibernate.database.connection.HibernateDriver"
args "diffChangeLog"
}

build.gradle 中添加 apply from: 'liquibase.gradle'

现在,要生成 diffChangeLog,请调用 gradle liquibaseDiffChangelog

请注意该示例使用 spring boot 的类之​​一作为 Hibernate Physical Naming Strategy。您可以引用 this answer 创建自己的实现。

关于spring - 如何使用 liquibase 在 spring 中从数据库和持久性实体之间的差异生成变更日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44862889/

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