从纯 Java 生成 jOOQ 类

Generate jOOQ classes from pure Java

有没有办法从纯 Java 代码生成 jOOQ 类?如果没有,最接近的选择是什么?理想情况下,我想在 gradle 构建中执行此操作。

我找到了this answer, which links to this blog post。 post的本质是这样的:

  1. 从 JPA 模型开始
  2. 将其转换为 DDL 脚本(.sql 文件充满 CREATE 语句)
  3. 创建一个新的 HSQLDB 文件,并通过 运行在其上运行 DDL 脚本来用表填充它。将生成的数据库保存到磁盘。
  4. 从磁盘加载该数据库并运行在其上生成 jOOQ 代码
  5. 使用生成的 jOOQ 类。

关于这种方法,有三点困扰我:

Jooq 需要事先创建一些表格。您可以为此使用 Flyway(您也应该使用它的迁移)。

一旦你拥有所有的表格,你可以使用这个片段让 Jooq 为你的表格生成源文件:

import org.jooq.util.GenerationTool;
import org.jooq.util.jaxb.*;

Configuration configuration = new Configuration()
    .withJdbc(new Jdbc()
        .withDriver("org.postgresql.Driver")
        .withUrl("jdbc:postgresql://localhost/your_database")
        .withUser("username")
        .withPassword("password"))
    .withGenerator(new Generator()
        .withName("org.jooq.util.DefaultGenerator")
        .withDatabase(new Database()
                .withName("org.jooq.util.postgres.PostgresDatabase")
                .withIncludes(".*")
                .withSchemata(new Schema().withInputSchema("your_schema"))
        )
        .withTarget(new Target()
            .withPackageName("jooq.generate")
            .withDirectory("src/main/java")));
try {
  GenerationTool.generate(configuration);
} catch (Exception e) {
  e.printStackTrace();
}

这将在 src/main/java path 上留下一些 .java 个文件。使用您的数据库设置等配置代码段

这大致就是我们在工作项目中做事的方式。 Jooq 与 Flyway. You can check Jooq's documentation about Code generation.

配合得很好

有一个我们不使用的 maven 插件,因为我们有一个多租户设置,需要一些 运行数据库迁移和代码生成的时间配置。

我想一旦你在一些 class 的静态 main 方法中准备好你的 Jooq 生成,你就可以 运行 它来自 gradle 任务。我只能从 gradle here

指向 Jooq 关于 运行ning 的文档

编辑:出于好奇查看文档和我自己生成的代码后,我发现您可以扩展 RecordTableImpl<Record> classes 生成您手动创建的模式 classes.

您可以在 RecordTable class 上看到 here 重要部分的小示例片段。

无论如何,请考虑到 Jooq(和 Flyway)不希望您避免 SQL 而是拥抱它。正常的做事方式是

  1. 您使用 SQL 脚本创建数据库对象
  2. 您使用 Jooq 生成器生成您的架构 classes
  3. 您将它们与 Jooq 的 DSL 上下文一起使用。

基于@ggalmazor 的 ,我最终使用了大部分 Java 代码,以及最少的 SQL 脚本核心。现在我的代码生成过程包含 3 个简单的步骤:

  1. 编写 SQL DDL 脚本(即 CREATE TABLE 语句)
  2. 运行Flyway,创建H2内存数据库
  3. 运行 jOOQ 在同一个数据库上生成 Java 代码。

Flyway 和 jOOQ 都在 Java 代码中完全配置;不需要 xml 个配置文件。

这是我的代码:

public class Main {

    public static void main(String... args) {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
        ds.setUser("u");
        ds.setPassword("p");

        // Flyway
        Flyway flyway = new Flyway();
        flyway.setDataSource(ds);
        flyway.setLocations("classpath:com.example.datadefinition.flyway.migrations");
        flyway.migrate();

        // jOOQ
        try (Connection c = ds.getConnection()) {
            Configuration configuration = new Configuration()
                .withGenerator(new Generator()
                    .withName(MyOwnGenerator.class.getCanonicalName())
                    .withDatabase(new Database()
                        .withName(H2Database.class.getCanonicalName())
                        .withIncludes(".*")
                        .withExcludes("")
                        .withInputSchema("PUBLIC")
                    )
                    .withTarget(new Target()
                        .withPackageName("com.example.lib.data")
                        // jOOQ will create package folders for com.example.lib.data
                        .withDirectory("../sibling-project/src/main")
                    )
                );

            GenerationTool tool = new GenerationTool();
            tool.setConnection(c);
            tool.run(configuration);
        } catch (SQLException e) {
            // sql connection problems
            e.printStackTrace();
        } catch (Exception e) {
            // run failed
            e.printStackTrace();
        }
    }
}

public class MyOwnGenerator extends JavaGenerator {

    public SwsGenerator() {
        setStrategy(new MyOwnGeneratorStrategy());
    }

}

public class MyOwnGeneratorStrategy extends DefaultGeneratorStrategy {

}