我正在使用 JOOQ 3.6.4 和 Java 8 从模式生成一组 JOOQ 记录。
一些表是结构相似的引用数据,假设它们有 ID、CODE 和 VALUE 列(它们可能有其他列,但它们都至少有这些列)。
在我的代码中,不是由 JOOQ 生成的,我有一个接口(interface)“ReferenceData”,它定义了与 JOOQ 为这三列生成的代码相匹配的访问器。我想告诉 JOOQ 添加一个 "implements ReferenceData"
子句到它生成的 Record 对象(JOOQ 已经生成的代码将自动实现接口(interface))。
我不是要求 JOOQ 自动找出接口(interface),我可以在 XML 配置中列出每个表应实现的接口(interface)。
问题 1:有什么方法可以配置 JOOQ 来生成 implements 子句而无需编写自定义生成器类?
如果我必须编写自定义生成器类 - 我仍然希望在 XML 配置中定义哪些表记录实现哪些接口(interface)。
问题 2:是否有在 XML 中定义自定义数据的示例,这些自定义数据向下传递到自定义生成器类中?
最佳答案
这可以用
- Generator strategies
- Matcher strategies (它们是内置的、基于 XML 的生成器策略)
生成器策略
使用生成器策略,您将实现以下代码:
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());
}
}
}
然后可以将上面的代码 Hook 到您的代码生成器配置中:
<generator>
<strategy>
<name>com.example.MyStrategy</name>
</strategy>
</generator>
匹配策略
使用匹配器策略,你基本上会写:
<generator>
<strategy>
<matchers>
<tables>
<table>
<expression>A_REGEX_MATCHING_ALL_RELEVANT_TABLES</expression>
<recordImplements>com.example.MyCustomInterface</recordImplements>
</table>
</tables>
</matchers>
</strategy>
</generator>
如您所见,对于像您这样的简单用例,匹配器策略比生成器策略更容易配置。
关于java - JOOQ:如何向生成的记录类添加接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33004232/