首页 > 解决方案 > 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 配置架构名称?

标签: javamysqlsqljooq

解决方案


您可以在代码生成时或运行时映射您的模式。这两个选项都包括根本不在生成的 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();

更多细节在这里


推荐阅读