Jdbc
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
此可选的顶级配置元素允许配置 JDBC 连接。 默认情况下,jOOQ 代码生成器需要一个活动的 JDBC 连接来逆向工程您的数据库模式。 例如,如果您想连接到 MySQL 数据库,请编写以下内容
<configuration> <jdbc> <driver>com.mysql.cj.jdbc.Driver</driver> <url>jdbc:mysql:///testdb</url> <!-- "username" is a valid synonym for "user" --> <user>root</user> <password>secret</password> </jdbc> </configuration>
有关更多详细信息,请参阅配置 XSD、独立代码生成和 Maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withJdbc(new Jdbc() .withDriver("com.mysql.cj.jdbc.Driver") .withUrl("jdbc:mysql:///testdb") // "username" is a valid synonym for "user" .withUser("root") .withPassword("secret") )
import org.jooq.meta.jaxb.* configuration { jdbc { driver = "com.mysql.cj.jdbc.Driver" url = "jdbc:mysql:///testdb" // "username" is a valid synonym for "user" user = "root" password = "secret" } }
有关更多详细信息,请参阅配置 XSD和 gradle 代码生成。
configuration { jdbc { driver = "com.mysql.cj.jdbc.Driver" url = "jdbc:mysql:///testdb" // "username" is a valid synonym for "user" user = "root" password = "secret" } }
有关更多详细信息,请参阅配置 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.
请注意,当通过 GenerationTool
使用编程式配置 API 时,您还可以将预先存在的 JDBC 连接传递给 GenerationTool
,而无需配置此元素。
请确保 JDBC 驱动程序可作为代码生成器依赖项提供给代码生成器
使用系统属性配置 JDBC 参数
一些配置属性可以使用系统属性进行配置。 特别是,JDBC URL 可能很重要通过系统属性进行配置,如果它是动态的,例如基于随机的 testcontainers 端口,并且如果 Maven/Gradle 可以避免在没有任何更改时启动容器,则不应急于配置
<configuration> <jdbc> <driver>com.mysql.cj.jdbc.Driver</driver> <!-- The -Dcom.example.myproperty system property contains the JDBC URL --> <urlProperty>com.example.myproperty</urlProperty> <!-- "username" is a valid synonym for "user" --> <user>root</user> <password>secret</password> </jdbc> </configuration>
有关更多详细信息,请参阅配置 XSD、独立代码生成和 Maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withJdbc(new Jdbc() .withDriver("com.mysql.cj.jdbc.Driver") // The -Dcom.example.myproperty system property contains the JDBC URL .withUrlProperty("com.example.myproperty") // "username" is a valid synonym for "user" .withUser("root") .withPassword("secret") )
import org.jooq.meta.jaxb.* configuration { jdbc { driver = "com.mysql.cj.jdbc.Driver" // The -Dcom.example.myproperty system property contains the JDBC URL urlProperty = "com.example.myproperty" // "username" is a valid synonym for "user" user = "root" password = "secret" } }
有关更多详细信息,请参阅配置 XSD和 gradle 代码生成。
configuration { jdbc { driver = "com.mysql.cj.jdbc.Driver" // The -Dcom.example.myproperty system property contains the JDBC URL urlProperty = "com.example.myproperty" // "username" is a valid synonym for "user" user = "root" password = "secret" } }
有关更多详细信息,请参阅配置 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.
现在,运行代码生成,并将 JDBC URL 指定为 -Dcom.example.myproperty=jdbc:mysql:///testdb
,例如。
可选 JDBC 属性
JDBC 驱动程序允许在创建连接时将 java.util.Properties
传递给 JDBC 驱动程序。 代码生成器配置也支持此功能,可以使用键/值对列表,如下所示
<configuration> <jdbc> <driver>com.mysql.cj.jdbc.Driver</driver> <url>jdbc:mysql:///testdb</url> <properties> <property> <key>user</key> <value>root</value> </property> <property> <key>password</key> <value>secret</value> </property> </properties> </jdbc> </configuration>
有关更多详细信息,请参阅配置 XSD、独立代码生成和 Maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withJdbc(new Jdbc() .withDriver("com.mysql.cj.jdbc.Driver") .withUrl("jdbc:mysql:///testdb") .withProperties( new Property() .withKey("user") .withValue("root"), new Property() .withKey("password") .withValue("secret") ) )
import org.jooq.meta.jaxb.* configuration { jdbc { driver = "com.mysql.cj.jdbc.Driver" url = "jdbc:mysql:///testdb" properties { property { key = "user" value = "root" } property { key = "password" value = "secret" } } } }
有关更多详细信息,请参阅配置 XSD和 gradle 代码生成。
configuration { jdbc { driver = "com.mysql.cj.jdbc.Driver" url = "jdbc:mysql:///testdb" properties { property { key = "user" value = "root" } property { key = "password" value = "secret" } } } }
有关更多详细信息,请参阅配置 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.
自动提交
jOOQ 的代码生成器将使用驱动程序/连接的默认自动提交标志。 如果由于某种原因您需要覆盖此设置(例如,为了通过将其设置为 true 来从 PostgreSQL 中失败的事务中恢复),您可以在此处指定它
<configuration> <jdbc> <autoCommit>true</autoCommit> </jdbc> </configuration>
有关更多详细信息,请参阅配置 XSD、独立代码生成和 Maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withJdbc(new Jdbc() .withAutoCommit(true) )
import org.jooq.meta.jaxb.* configuration { jdbc { isAutoCommit = true } }
有关更多详细信息,请参阅配置 XSD和 gradle 代码生成。
configuration { jdbc { autoCommit = true } }
有关更多详细信息,请参阅配置 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.
初始化脚本
您可以选择提供一个脚本,在创建 JDBC 连接之后以及运行代码生成器之前运行。
<configuration> <jdbc> <initScript>CREATE SCHEMA X//SET SCHEMA X</initScript> <!-- The separator between statements, defaulting to ";" --> <initSeparator>//</initSeparator> </jdbc> </configuration>
有关更多详细信息,请参阅配置 XSD、独立代码生成和 Maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withJdbc(new Jdbc() .withInitScript("CREATE SCHEMA X//SET SCHEMA X") // The separator between statements, defaulting to ";" .withInitSeparator("//") )
import org.jooq.meta.jaxb.* configuration { jdbc { initScript = "CREATE SCHEMA X//SET SCHEMA X" // The separator between statements, defaulting to ";" initSeparator = "//" } }
有关更多详细信息,请参阅配置 XSD和 gradle 代码生成。
configuration { jdbc { initScript = "CREATE SCHEMA X//SET SCHEMA X" // The separator between statements, defaulting to ";" initSeparator = "//" } }
有关更多详细信息,请参阅配置 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.
何时 JDBC 配置是可选的
在某些情况下,JDBC 连接不需要配置,例如,当使用 JPADatabase(从 JPA 注解的实体中进行逆向工程)或使用 XMLDatabase(从 XML 文件中进行逆向工程)时。 请参阅相应的章节以获取更多详细信息。
反馈
您对此页面有任何反馈吗? 我们很乐意听到您的反馈!