我对多个数据库(和数据库服务器)有完全相同的数据库定义。如何告诉 Jooq 使用与我创建的“连接”相同的数据库来连接到数据库?
示例(对于 MySQL):
- jdbc:mysql://localhost:3306/tsm - 我的开发数据库 ( tsm ),用于生成代码
- jdbc:mysql://RemoteAmazonDBHost:3306/customer1 - 我的客户之一
- jdbc:mysql://RemoteAmazonDBHost:3306/customer2 - 另一个客户
所有 3 个数据库具有相同的定义、相同的表、索引等。TSM 数据库是我们的应用程序使用的标准。
也许我应该使用DSL.using(Connection,Setting)
而不是DSL.using(Connection)
?这就是手册的含义吗here ?
如果我只有一个“输入”模式,我是否必须指定它?换句话说,我可以做这样的事情:
Settings settings = new Settings()
.withRenderMapping(new RenderMapping()
.withSchemata(
new MappedSchema().withOutput(
databaseInfo.getProperties().getProperty("database.db"))));
或者我必须做这样的事情:
Settings settings = new Settings()
.withRenderMapping(new RenderMapping()
.withSchemata(
new MappedSchema().withInput("TSM")
.withOutput(databaseInfo.getProperties().getProperty("database.db"))));
最佳答案
我假设您正在使用代码生成。在这种情况下,根本不生成架构而是使用 <outputSchemaToDefault>
可能是最简单的方法。在代码生成配置中,例如
<configuration>
<generator>
<database>
<inputSchema>your_codegen_input_schema_here</inputSchema>
<outputSchemaToDefault>true</outputSchemaToDefault>
</database>
</generator>
</configuration>
如果您想保留生成的代码具有模式限定并在运行时映射事物,那么您的第二次尝试似乎是正确的。将此信息传递给您的Configuration
(即 DSL.using()
调用):
Settings settings = new Settings()
.withRenderMapping(new RenderMapping()
.withSchemata(new MappedSchema()
.withInput("TSM")
.withOutput(databaseInfo.getProperties().getProperty("database.db"))));
更多详细信息可以在这里找到:https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-render-mapping
关于mysql - jooq指定数据库运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53000414/