LiquibaseDatabase:从 Liquibase XML、YAML、JSON 文件生成代码
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
如果您正在使用 Liquibase,您将把数据库定义为一组 Liquibase 变更集,格式为 XML、YAML 或 JSON。该数据库定义是完整的、自包含的,可以很容易地被 jOOQ 代码生成器用作元信息源。
例如,可以使用以下 database.xml 脚本(YAML 或 JSON 格式请参考 Liquibase 文档)
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="authorName" id="changelog-1.0">
<createTable tableName="MY_TABLE">
<column name="MY_COLUMN" type="TEXT">
<constraints nullable="true" primaryKey="false" unique="false" />
</column>
</createTable>
</changeSet>
</databaseChangeLog>
为了将上述脚本用作 jOOQ 代码生成器的元数据来源,您只需按如下方式设置代码生成配置
<configuration>
<generator>
<database>
<name>org.jooq.meta.extensions.liquibase.LiquibaseDatabase</name>
<properties>
<!-- Specify the classpath location of your XML, YAML, or JSON script. -->
<property>
<key>scripts</key>
<value>/database.xml</value>
</property>
<!-- Whether you want to include liquibase tables in generated output
- false (default)
- true: includes DATABASECHANGELOG and DATABASECHANGELOGLOCK tables -->
<property>
<key>includeLiquibaseTables</key>
<value>false</value>
</property>
<!-- Whether you want to use jOOQ's translating ParsingConnection to translate
between your dialect (e.g. Oracle), and jOOQ's in-memory H2 dialect
- false (default)
- true: translates e.g. from VARCHAR2(100) to VARCHAR(100) -->
<property>
<key>useParsingConnection</key>
<value>false</value>
</property>
<!-- Properties prefixed "database." will be passed on to the liquibase.database.Database class
if a matching setter is found -->
<property>
<key>database.liquibaseSchemaName</key>
<value>lb</value>
</property>
<!-- The property "changeLogParameters.contexts" will be passed on to the
liquibase.database.Database.update() call (jOOQ 3.13.2+).
See https://www.liquibase.org/documentation/contexts.html -->
<property>
<key>changeLogParameters.contexts</key>
<value>!test</value>
</property>
</properties>
</database>
</generator>
</configuration>
更多详情请参阅配置 XSD、独立代码生成以及Maven 代码生成。
new org.jooq.meta.jaxb.Configuration()
.withGenerator(new Generator()
.withDatabase(new Database()
.withName("org.jooq.meta.extensions.liquibase.LiquibaseDatabase")
.withProperties(
// Specify the classpath location of your XML, YAML, or JSON script.
new Property()
.withKey("scripts")
.withValue("/database.xml"),
// Whether you want to include liquibase tables in generated output
//
// - false (default)
// - true: includes DATABASECHANGELOG and DATABASECHANGELOGLOCK tables
new Property()
.withKey("includeLiquibaseTables")
.withValue(false),
// Whether you want to use jOOQ's translating ParsingConnection to translate
// between your dialect (e.g. Oracle), and jOOQ's in-memory H2 dialect
//
// - false (default)
// - true: translates e.g. from VARCHAR2(100) to VARCHAR(100)
new Property()
.withKey("useParsingConnection")
.withValue(false),
// Properties prefixed "database." will be passed on to the liquibase.database.Database class
// if a matching setter is found
new Property()
.withKey("database.liquibaseSchemaName")
.withValue("lb"),
// The property "changeLogParameters.contexts" will be passed on to the
// liquibase.database.Database.update() call (jOOQ 3.13.2+).
// See https://www.liquibase.org/documentation/contexts.html
new Property()
.withKey("changeLogParameters.contexts")
.withValue("!test")
)
)
)
import org.jooq.meta.jaxb.*
configuration {
generator {
database {
name = "org.jooq.meta.extensions.liquibase.LiquibaseDatabase"
properties {
// Specify the classpath location of your XML, YAML, or JSON script.
property {
key = "scripts"
value = "/database.xml"
}
// Whether you want to include liquibase tables in generated output
//
// - false (default)
// - true: includes DATABASECHANGELOG and DATABASECHANGELOGLOCK tables
property {
key = "includeLiquibaseTables"
isValue = false
}
// Whether you want to use jOOQ's translating ParsingConnection to translate
// between your dialect (e.g. Oracle), and jOOQ's in-memory H2 dialect
//
// - false (default)
// - true: translates e.g. from VARCHAR2(100) to VARCHAR(100)
property {
key = "useParsingConnection"
isValue = false
}
// Properties prefixed "database." will be passed on to the liquibase.database.Database class
// if a matching setter is found
property {
key = "database.liquibaseSchemaName"
value = "lb"
}
// The property "changeLogParameters.contexts" will be passed on to the
// liquibase.database.Database.update() call (jOOQ 3.13.2+).
// See https://www.liquibase.org/documentation/contexts.html
property {
key = "changeLogParameters.contexts"
value = "!test"
}
}
}
}
}
更多详情请参阅配置 XSD和Gradle 代码生成。
configuration {
generator {
database {
name = "org.jooq.meta.extensions.liquibase.LiquibaseDatabase"
properties {
// Specify the classpath location of your XML, YAML, or JSON script.
property {
key = "scripts"
value = "/database.xml"
}
// Whether you want to include liquibase tables in generated output
//
// - false (default)
// - true: includes DATABASECHANGELOG and DATABASECHANGELOGLOCK tables
property {
key = "includeLiquibaseTables"
value = false
}
// Whether you want to use jOOQ's translating ParsingConnection to translate
// between your dialect (e.g. Oracle), and jOOQ's in-memory H2 dialect
//
// - false (default)
// - true: translates e.g. from VARCHAR2(100) to VARCHAR(100)
property {
key = "useParsingConnection"
value = false
}
// Properties prefixed "database." will be passed on to the liquibase.database.Database class
// if a matching setter is found
property {
key = "database.liquibaseSchemaName"
value = "lb"
}
// The property "changeLogParameters.contexts" will be passed on to the
// liquibase.database.Database.update() call (jOOQ 3.13.2+).
// See https://www.liquibase.org/documentation/contexts.html
property {
key = "changeLogParameters.contexts"
value = "!test"
}
}
}
}
}
更多详情请参阅配置 XSD和Gradle 代码生成。
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19. // Please use the official plugin instead of the third party plugin that was recommended before.
如果您不想使用 Liquibase ClassLoaderResourceAccessor,而是想使用 FileSystemResourceAccessor,则需要从 jOOQ 3.16 开始提供一个 rootPath,该版本依赖于 Liquibase 4(或更高版本)。
<configuration>
<generator>
<database>
<name>org.jooq.meta.extensions.liquibase.LiquibaseDatabase</name>
<properties>
<!-- Specify the root path, e.g. a path in your Maven directory layout -->
<property>
<key>rootPath</key>
<value>${basedir}/src/main/resources</value>
</property>
<!-- Specify the relative path location of your XML, YAML, or JSON script. -->
<property>
<key>scripts</key>
<value>database.xml</value>
</property>
</properties>
</database>
</generator>
</configuration>
更多详情请参阅配置 XSD、独立代码生成以及Maven 代码生成。
new org.jooq.meta.jaxb.Configuration()
.withGenerator(new Generator()
.withDatabase(new Database()
.withName("org.jooq.meta.extensions.liquibase.LiquibaseDatabase")
.withProperties(
// Specify the root path, e.g. a path in your Maven directory layout
new Property()
.withKey("rootPath")
.withValue("${basedir}/src/main/resources"),
// Specify the relative path location of your XML, YAML, or JSON script.
new Property()
.withKey("scripts")
.withValue("database.xml")
)
)
)
import org.jooq.meta.jaxb.*
configuration {
generator {
database {
name = "org.jooq.meta.extensions.liquibase.LiquibaseDatabase"
properties {
// Specify the root path, e.g. a path in your Maven directory layout
property {
key = "rootPath"
value = "${basedir}/src/main/resources"
}
// Specify the relative path location of your XML, YAML, or JSON script.
property {
key = "scripts"
value = "database.xml"
}
}
}
}
}
更多详情请参阅配置 XSD和Gradle 代码生成。
configuration {
generator {
database {
name = "org.jooq.meta.extensions.liquibase.LiquibaseDatabase"
properties {
// Specify the root path, e.g. a path in your Maven directory layout
property {
key = "rootPath"
value = "${basedir}/src/main/resources"
}
// Specify the relative path location of your XML, YAML, or JSON script.
property {
key = "scripts"
value = "database.xml"
}
}
}
}
}
更多详情请参阅配置 XSD和Gradle 代码生成。
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19. // Please use the official plugin instead of the third party plugin that was recommended before.
依赖
请注意,org.jooq.meta.extensions.liquibase.LiquibaseDatabase 类位于一个外部依赖项中,需要将其放置在 jOOQ 代码生成器的类路径中。 例如,使用 Maven
<dependency>
<!-- Use org.jooq for the Open Source Edition
org.jooq.pro for commercial editions with Java 21 support,
org.jooq.pro-java-17 for commercial editions with Java 17 support,
org.jooq.pro-java-11 for commercial editions with Java 11 support,
org.jooq.pro-java-8 for commercial editions with Java 8 support,
org.jooq.trial for the free trial edition with Java 21 support,
org.jooq.trial-java-17 for the free trial edition with Java 17 support,
org.jooq.trial-java-11 for the free trial edition with Java 11 support,
org.jooq.trial-java-8 for the free trial edition with Java 8 support
Note: Only the Open Source Edition is hosted on Maven Central.
Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk -->
<groupId>org.jooq</groupId>
<artifactId>jooq-meta-extensions-liquibase</artifactId>
<version>3.20.5</version>
</dependency>
dependencies {
// Use org.jooq for the Open Source Edition
// org.jooq.pro for commercial editions with Java 21 support,
// org.jooq.pro-java-17 for commercial editions with Java 17 support,
// org.jooq.pro-java-11 for commercial editions with Java 11 support,
// org.jooq.pro-java-8 for commercial editions with Java 8 support,
// org.jooq.trial for the free trial edition with Java 21 support,
// org.jooq.trial-java-17 for the free trial edition with Java 17 support,
// org.jooq.trial-java-11 for the free trial edition with Java 11 support,
// org.jooq.trial-java-8 for the free trial edition with Java 8 support
//
// Note: Only the Open Source Edition is hosted on Maven Central.
// Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
// See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk
jooqCodegen("org.jooq:jooq-meta-extensions-liquibase:3.20.5")
}
dependencies {
// Use org.jooq for the Open Source Edition
// org.jooq.pro for commercial editions with Java 21 support,
// org.jooq.pro-java-17 for commercial editions with Java 17 support,
// org.jooq.pro-java-11 for commercial editions with Java 11 support,
// org.jooq.pro-java-8 for commercial editions with Java 8 support,
// org.jooq.trial for the free trial edition with Java 21 support,
// org.jooq.trial-java-17 for the free trial edition with Java 17 support,
// org.jooq.trial-java-11 for the free trial edition with Java 11 support,
// org.jooq.trial-java-8 for the free trial edition with Java 8 support
//
// Note: Only the Open Source Edition is hosted on Maven Central.
// Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
// See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk
jooqCodegen "org.jooq:jooq-meta-extensions-liquibase:3.20.5"
}
Liquibase 可能需要其他依赖项,例如在使用 YAML 时。 请参阅 Liquibase 文档了解有关其他依赖项的信息。
特定于供应商的功能
LiquibaseDatabase 在内存中模拟您的迁移,因此可能不支持目标方言的所有特定于供应商的功能。 如果您遇到此类限制,请知悉这只是一种针对此类模拟的“快速而肮脏”的方法。 在您实际的目标 RDBMS 上运行实际的迁移(例如,使用 testcontainers)将更可靠。 此处提供了一个此方法的示例:https://blog.jooq.org/using-testcontainers-to-generate-jooq-code/。
反馈
您对此页面有任何反馈吗? 我们很乐意听到您的反馈!