简体   繁体   English

通过 Java API 使用 Liquibase 生成数据导出 SQL 文件

[英]Generate Data Export SQL File Using Liquibase via Java API

would like to generate an SQL file from liquibase via Java code replicating this command想通过复制此命令的 Java 代码从 liquibase 生成 SQL 文件

java -jar liquibase.jar --changeLogFile="./data/filename" --diffTypes="data" generateChangeLog

I am using this as a replacement for a home grown SQL backup Java class我正在使用它来替代本地开发的 SQL 备份 Java 类

Something like the following should do what you want I think:像下面这样的事情应该做你想做的我认为:

ResourceAccessor resourceAccessor = new FileSystemResourceAccessor();
Database db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(yourJdbcConnection));
Liquibase liquibase = new Liquibase("data/filename", resourceAccessor, db);
try (Writer writer = new OutputStreamWriter(System.out)) {
    liquibase.update((Contexts) null, writer);
}

You can use an util method liquibase.integration.commandline.CommandLineUtils#doGenerateChangeLog from the liquibase-core.jar .您可以使用liquibase-core.jarliquibase-core.jar方法liquibase.integration.commandline.CommandLineUtils#doGenerateChangeLog It uses same parameters as in your example with a command line.它使用与命令行示例中相同的参数。

In my project there is example of using this method:https://github.com/0x100/liquibase-backuper-spring-boot-starter .在我的项目中有使用这种方法的例子:https ://github.com/0x100/liquibase-backuper-spring-boot-starter This is a starter for Spring Boot, but I think it's easy to understand how to use the method.这是Spring Boot的入门,但我认为很容易理解如何使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM