简体   繁体   English

JOOQ:如何向生成的记录类添加接口

[英]JOOQ: how do I add an interface to a generated Record Class

I am generating a set of JOOQ records from a schema using JOOQ 3.6.4 with Java 8. 我正在使用JOOQ 3.6.4和Java 8从模式生成一组JOOQ记录。

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). 一些表是类似结构的参考数据,假设它们有ID,CODE和VALUE列(它们可能有其他列,但它们都至少有这些列)。

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. 在我的代码中,不是由JOOQ生成的,我有一个接口“ReferenceData”,它定义了与JOOQ为这三列生成的代码匹配的访问器。 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). 我想告诉JOOQ将"implements ReferenceData"子句添加到它生成的Record对象中(JOOQ已生成的代码将自动实现接口)。

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. 我不是要求JOOQ自动找出接口,我很好地列出了每个表应该在XML配置中实现的接口。

Question 1 : is there any way to configure JOOQ to generate an implements clause without writing a custom generator class? 问题1 :有没有办法配置JOOQ来生成implements子句而无需编写自定义生成器类?

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. 如果我必须编写一个自定义生成器类 - 我仍然希望定义什么表记录实现XML配置中的接口。

Question 2 : Is there an example somewhere of defining custom data in the XML that is communicated down into the custom generator class? 问题2 :是否有一个在XML中定义自定义数据的示例,该数据被传递到自定义生成器类中?

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. 正如您所看到的,对于像您这样的简单用例,匹配器策略比生成器策略更容易配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM