简体   繁体   English

JOOQ - 将结果转换为Pojo

[英]JOOQ - convert result into Pojo

I have seen that JOOQ can automatically return a POJO when we use .selectFrom(TABLE) or .fetchInto(POJO.class); 我已经看到,当我们使用.selectFrom(TABLE).fetchInto(POJO.class);时, .selectFrom(TABLE)可以自动返回POJO .fetchInto(POJO.class);

But is it possible to convert the result of a complex query into multiple POJO ? 但是有可能将复杂查询的结果转换为多个POJO吗?

Example : 示例:

This query will return an array of all columns into tables Support and Box. 此查询将所有列的数组返回到表Support和Box中。 It is possible to convert them into a Support and Box Pojo ? 可以将它们转换为Support and Box Pojo吗?

Result<Record> results = query.select()
                         .from(BOX)
                         .join(SUPPORT)
                         .on(SUPPORT.ID.equal(BOX.SUPPORT_ID))
                         .where(SUPPORT.ID.equal("XXXX"))
                         .orderBy(BOX.ID)
                         .fetch();

I have tested the method .intoGroups(SUPPORT.ID, Box.class) , it works fine. 我已经测试了.intoGroups(SUPPORT.ID, Box.class) ,它可以正常工作。 But I doesn't have the support object. 但我没有支持对象。

Instantiate to SelectSeekStep1 实例化为SelectSeekStep1

With aliases it's more convenient: 使用别名更方便:

Box b = BOX.as("b");
Support s = SUPPORT.as("s");

SelectSeekStep1<Integer, Integer> sql = query.select(b.ID, s.ID /* other columns */)
     .from(b)
     .join(s)
        .on(s.ID.eq(b.SUPPORT_ID))
     .where(s.ID.eq("XXXX"))
     .orderBy(b.ID)
;

Then just fetch what/as you need: 然后只需获取您需要的内容:

List<BoxRecord> boxes = sql.fetchInto(BOX); 
SupportRecord support = sql.limit(1).fetchOneInto(SUPPORT);

For future readers, if you want to achieve the same behaviour with insert methods you should use: 对于未来的读者,如果要使用插入方法实现相同的行为,则应使用:

insertInto(BOX)
    .set(BOX.COLUMN1, UInteger.valueOf(1))
    .set(BOX.COLUMN2, "test")
    .returning()
    .fetchOne()
    .into(<POJO_class>.class);

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

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