本文介绍了JOOQ:如何将接口添加到生成的记录类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java 8的JOOQ 3.6.4从架构中生成一组JOOQ记录.

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

某些表是结构相似的参考数据,假设它们具有ID,CODE和VALUE列(它们可能具有其他列,但它们至少都具有这些列).

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).

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

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).

我并不是要让JOOQ自动找出接口,我很好地列出了每个表应在XML配置中实现的接口.

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.

问题1 :是否可以在不编写自定义生成器类的情况下配置JOOQ来生成Implements子句?

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

如果我必须编写一个自定义生成器类-我仍然希望定义哪些表记录实现XML配置中要包含的接口.

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.

问题2 :是否有示例定义在XML中定义的自定义数据,该数据向下传递到自定义生成器类中?

Question 2: Is there an example somewhere of defining custom data in the XML that is communicated down into the custom generator class?

推荐答案

这可以使用

  • Generator strategies
  • Matcher strategies (which are built-in, XML-based generator strategies)

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

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.

这篇关于JOOQ:如何将接口添加到生成的记录类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:48