XMLDatabase:从 XML 文件生成代码
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
默认情况下,jOOQ 的代码生成器使用实时数据库连接作为数据库元数据源。在许多项目设置中,这可能不是最佳选择,因为实时数据库并不总是可用。
解决此问题的一种方法是为 jOOQ 提供 XML 格式的数据库元定义文件,并将此 XML 文件传递给 jOOQ 的 XMLDatabase。
XMLDatabase 可以读取实现 https://jooq.org.cn/xsd/jooq-meta-3.20.0.xsd 模式的标准化 XML 文件。 本质上,此模式是 SQL 标准 INFORMATION_SCHEMA 的 XML 表示形式,由 H2、HSQLDB、MySQL、PostgreSQL 或 SQL Server 等数据库实现。
下面可以看到一个包含简单模式、表、列定义的示例模式定义
<?xml version="1.0"?>
<information_schema xmlns="https://jooq.org.cn/xsd/jooq-meta-3.20.0.xsd">
<schemata>
<schema>
<schema_name>TEST</schema_name>
</schema>
</schemata>
<tables>
<table>
<table_schema>TEST</table_schema>
<table_name>AUTHOR</table_name>
</table>
<table>
<table_schema>TEST</table_schema>
<table_name>BOOK</table_name>
</table>
</tables>
<columns>
<column>
<table_schema>PUBLIC</table_schema>
<table_name>AUTHOR</table_name>
<column_name>ID</column_name>
<data_type>NUMBER</data_type>
<numeric_precision>7</numeric_precision>
<ordinal_position>1</ordinal_position>
<is_nullable>false</is_nullable>
</column>
<!-- ... -->
</columns>
</information_schema>
可以使用以下元素定义约束
<?xml version="1.0"?>
<information_schema xmlns="https://jooq.org.cn/xsd/jooq-meta-3.20.0.xsd">
<table_constraints>
<table_constraint>
<constraint_schema>TEST</constraint_schema>
<constraint_name>PK_AUTHOR</constraint_name>
<constraint_type>PRIMARY KEY</constraint_type>
<table_schema>TEST</table_schema>
<table_name>AUTHOR</table_name>
</table_constraint>
<!-- ... -->
</table_constraints>
<key_column_usages>
<key_column_usage>
<constraint_schema>TEST</constraint_schema>
<constraint_name>PK_AUTHOR</constraint_name>
<table_schema>TEST</table_schema>
<table_name>AUTHOR</table_name>
<column_name>ID</column_name>
<ordinal_position>1</ordinal_position>
</key_column_usage>
<!-- ... -->
</key_column_usages>
<referential_constraints>
<referential_constraint>
<constraint_schema>TEST</constraint_schema>
<constraint_name>FK_BOOK_AUTHOR_ID</constraint_name>
<unique_constraint_schema>TEST</unique_constraint_schema>
<unique_constraint_name>PK_AUTHOR</unique_constraint_name>
</referential_constraint>
<!-- ... -->
</referential_constraints>
</information_schema>
通过使用 XMLDatabase,可以将以上文件提供给代码生成器配置,如下所示
<configuration>
<generator>
<database>
<name>org.jooq.meta.xml.XMLDatabase</name>
<properties>
<!-- Use any of the SQLDialect values here -->
<property>
<key>dialect</key>
<value>ORACLE</value>
</property>
<!-- Specify the location of your database file -->
<property>
<key>xmlFile</key>
<value>src/main/resources/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.xml.XMLDatabase")
.withProperties(
// Use any of the SQLDialect values here
new Property()
.withKey("dialect")
.withValue("ORACLE"),
// Specify the location of your database file
new Property()
.withKey("xmlFile")
.withValue("src/main/resources/database.xml")
)
)
)
import org.jooq.meta.jaxb.*
configuration {
generator {
database {
name = "org.jooq.meta.xml.XMLDatabase"
properties {
// Use any of the SQLDialect values here
property {
key = "dialect"
value = "ORACLE"
}
// Specify the location of your database file
property {
key = "xmlFile"
value = "src/main/resources/database.xml"
}
}
}
}
}
有关更多详细信息,请参阅配置 XSD和gradle 代码生成。
configuration {
generator {
database {
name = "org.jooq.meta.xml.XMLDatabase"
properties {
// Use any of the SQLDialect values here
property {
key = "dialect"
value = "ORACLE"
}
// Specify the location of your database file
property {
key = "xmlFile"
value = "src/main/resources/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.
如果您的数据库已经有不同的 XML 格式,您可以借助额外的 Maven 插件将您自己的格式 XSL 转换为上述格式,或者通过提供额外的属性将 XSL 文件的位置传递给 XMLDatabase
<configuration>
<generator>
<database>
<name>org.jooq.meta.xml.XMLDatabase</name>
<properties>
<!-- Specify the location of your xsl file -->
<property>
<key>xslFile</key>
<value>src/main/resources/transform-to-jooq-format.xsl</value>
</property>
</properties>
</database>
</generator>
</configuration>
有关更多详细信息,请参阅配置 XSD,独立代码生成和maven 代码生成。
new org.jooq.meta.jaxb.Configuration()
.withGenerator(new Generator()
.withDatabase(new Database()
.withName("org.jooq.meta.xml.XMLDatabase")
.withProperties(
// Specify the location of your xsl file
new Property()
.withKey("xslFile")
.withValue("src/main/resources/transform-to-jooq-format.xsl")
)
)
)
import org.jooq.meta.jaxb.*
configuration {
generator {
database {
name = "org.jooq.meta.xml.XMLDatabase"
properties {
// Specify the location of your xsl file
property {
key = "xslFile"
value = "src/main/resources/transform-to-jooq-format.xsl"
}
}
}
}
}
有关更多详细信息,请参阅配置 XSD和gradle 代码生成。
configuration {
generator {
database {
name = "org.jooq.meta.xml.XMLDatabase"
properties {
// Specify the location of your xsl file
property {
key = "xslFile"
value = "src/main/resources/transform-to-jooq-format.xsl"
}
}
}
}
}
有关更多详细信息,请参阅配置 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.
现在可以检入和版本化此 XML 配置,并独立于实时数据库模式对其进行修改。
反馈
您对此页面有任何反馈吗? 我们很乐意听到您的反馈!