找到你要的答案

Q:JOOQ: how do I add an interface to a generated Record Class

Q:jooq:我如何添加一个接口生成的记录类

I am generating a set of JOOQ records from a schema using JOOQ 3.6.4 with Java 8.

Some of the tables are reference data that are similarly structured, let's say they have ID, CODE and VALUE columns (they might have other columns, but they all have at least these columns).

In my code, not generated by JOOQ, I have an interface "ReferenceData" that defines accessors that match the code that JOOQ generates for these three columns. I want to tell JOOQ to add an "implements ReferenceData" clause to the Record objects it generates (the code that JOOQ already generates will automatically implement the interfaces).

I'm not asking that JOOQ automatically figure out the interfaces, I'm fine with listing what interfaces each table should implement in the XML configuration.

Question 1: is there any way to configure JOOQ to generate an implements clause without writing a custom generator class?

If I have to write a custom generator class - I still want the definition of what table records implements what interfaces to be in the XML config.Question 2: Is there an example somewhere of defining custom data in the XML that is communicated down into the custom generator class?

我是生成一组从架构使用jooq 3.6.4与java 8 jooq记录。

有些表是类似结构的引用数据,比如说它们有id、代码和值列(它们可能有其他列,但它们至少都有这些列)。

在我的代码中,通过jooq不会产生,我有一个接口“参考”定义,这三jooq列生成代码匹配的访问器。我想告诉jooq添加实行“参考”条款来记录物体的产生(即jooq已经产生了将自动实现接口的代码)。

我没有问,jooq自动出图的界面,我可以列出接口的每个表应该在XML配置实现。

问题1:有什么办法来配置jooq没有自定义生成器类写作产生了条款?

如果我必须写一个自定义的生成器类-我仍然想要的定义表记录实现什么接口是在XML配置的问题2:是否有一个例子定义自定义数据在XML是沟通到自定义生成器类?

answer1: 回答1:

This can be done using

Generator strategy

With a generator strategy, you'll implement the following code:

public class MyStrategy extends DefaultGeneratorStrategy {
    @Override
    public List<String> getJavaClassImplements(Definition definition, Mode mode) {
        if (mode == Mode.RECORD && definition.getQualifiedName().matches("some regex")) {
            return Arrays.asList(MyCustomInterface.class.getName());
        }
    }
}

The above can then be hooked into your code generator configuration as such:

<generator>
  <strategy>
    <name>com.example.MyStrategy</name>
  </strategy>
</generator>

Matcher strategy

With a matcher strategy, you'll essentially write:

<generator>
  <strategy>
    <matchers>
      <tables>
        <table>
          <expression>A_REGEX_MATCHING_ALL_RELEVANT_TABLES</expression>
          <recordImplements>com.example.MyCustomInterface</recordImplements>
        </table>
      </tables>
    </matchers>
  </strategy>
</generator>

As you can see, matcher strategies are easier to configure than generator strategy, for simple use-cases like yours.

这可以用

Generator strategy

使用生成器策略,您将实现以下代码:

public class MyStrategy extends DefaultGeneratorStrategy {
    @Override
    public List<String> getJavaClassImplements(Definition definition, Mode mode) {
        if (mode == Mode.RECORD && definition.getQualifiedName().matches("some regex")) {
            return Arrays.asList(MyCustomInterface.class.getName());
        }
    }
}

然后可以将上述代码插入到代码生成器配置中:

<generator>
  <strategy>
    <name>com.example.MyStrategy</name>
  </strategy>
</generator>

Matcher strategy

一个匹配的策略,你就基本上写:

<generator>
  <strategy>
    <matchers>
      <tables>
        <table>
          <expression>A_REGEX_MATCHING_ALL_RELEVANT_TABLES</expression>
          <recordImplements>com.example.MyCustomInterface</recordImplements>
        </table>
      </tables>
    </matchers>
  </strategy>
</generator>

你可以看到,匹配策略比发电机的策略更容易配置,简单的用例像你。

java  sql  code-generation  jooq