java - jOOQ Codegen 具有用于开发、测试和生产的不同 MySQL 数据库
问题描述
我将 jOOQ 与 MySQL 一起使用,并且每个阶段(开发、测试、产品)具有不同的数据库。
pom.xml 中的配置如下所示。
<generator>
<database>
<name>org.jooq.meta.mysql.MySQLDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<inputSchema>db_test</inputSchema>
</database>
<target>
<packageName>ch.bls.nfdb.db</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
jOOQ 使用 inputSchema 并生成一个类 DbTest。
所有 SQL 语句都以该模式名称为前缀。这就是问题所在。如何为我的 prod 数据库 db_prod 配置架构名称?
解决方案
您可以在代码生成时或运行时映射您的模式。这两个选项都包括根本不在生成的 SQL 中呈现模式名称:
在代码生成时
<database>
<name>org.jooq.meta.mysql.MySQLDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<inputSchema>db_test</inputSchema>
<!-- Add this: -->
<outputSchemaToDefault>true</outputSchemaToDefault>
<!-- Or this: -->
<outputSchema>db_prod</outputSchema>
</database>
在运行时
Settings settings;
// Add this
settings = new Settings().withRenderSchema(false);
// Or this
settings = new Settings().withRenderMapping(
new RenderMapping().withSchemata(
new MappedSchema().withInput("db_dev").withOutput("db_prod")));
DSLContext ctx = DSL.using(connection, MYSQL, settings);
ctx.select().from(...).fetch();
推荐阅读
- dialog - 模拟 Vaadin 8 窗口标题的 Vaadin 10 对话框
- c - 如何从 C 中的完整路径(字符串)将文本附加到文件名?
- python - 我希望 Django orm 从 dB 表中找到当前日期和日期时间字段之间的天数差异
- android - Android:使用迭代器解析 JSONArray 键
- php - Facebook Messenger 重复发送用户输入
- php - 检查在 UPDATE 查询中修改了哪些列
- perl - Perl:在多个目录中的多个文件上运行脚本
- php - 在 CakePHP 3 中更新/插入关联表数据
- python - Python 3 整数地址
- javascript - 如何在点击提交按钮之前检测我被点击的位置