可用版本: Dev (3.21) | 最新 (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11

投影类型安全

适用于 ✅ 开源版   ✅ 专业版   ✅ 企业版

自 jOOQ 3.0 起,最高 22 度的记录行值表达式现在在类型上都是安全的。这通过 DSL 和 DSLContext 中重载的 SELECT(和 SELECT DISTINCT)API 反映出来。来自 DSL 类型的提取

// Non-typesafe select methods:
public static SelectSelectStep<Record> select(Collection<? extends SelectField<?>> fields);
public static SelectSelectStep<Record> select(SelectField<?>... fields);

// Typesafe select methods:
public static <T1>         SelectSelectStep<Record1<T1>>         select(SelectField<T1> field1);
public static <T1, T2>     SelectSelectStep<Record2<T1, T2>>     select(SelectField<T1> field1, SelectField<T2> field2);
// [...]

正在投影的类型是 org.jooq.SelectField,另请参见关于 SelectField 的下一节。由于泛型 R 类型绑定到某个 Record[N],因此关联的 T 类型信息可以在各种其他上下文中使用,例如 IN 谓词。这样的 SELECT 语句可以类型安全地分配

Select<Record2<Integer, String>> s1 = create.select(BOOK.ID, BOOK.TITLE);
Select<Record2<Integer, String>> s2 = create.select(BOOK.ID, trim(BOOK.TITLE));

// Alternatively, just use var to infer the type:
var s3 = create.select(BOOK.ID, trim(BOOK.TITLE));

有关度数高达 22 的类型安全记录类型的更多信息,请参见手册中关于Record1 到 Record22的部分。

反馈

您对此页面有任何反馈吗?我们很乐意听到!

The jOOQ Logo