【问题标题】:Map fetch/result from jooq to specific Record将 jooq 中的获取/结果映射到特定记录
【发布时间】:2013-07-15 05:04:05
【问题描述】:

当我当前使用 Jooq 进行查询时,我明确地将每个记录对象转换为预期的记录类型。

Result<Record> result = sql.select().from(Tables.COUNTRY).fetch();
for (Record r : result) {
    CountryRecord countryRecord = (CountryRecord) r;
    //Extract data from countryRecord
    countryRecord.getId();
}

是否可以使用 Jooq 将结果直接转换为所需的记录类型?

如(这样不编译):

Result<CountryRecord> countryRecords = (Result<CountryRecord>) sql.select().from(Tables.COUNTRY).fetch();
for (CountryRecord cr : countryRecords) {
    cr.getNamet();
    //etc...
}

【问题讨论】:

    标签: java casting jooq


    【解决方案1】:

    @卢卡斯,

    实际上我们使用fetchInto() 将结果转换为对象列表。

    例如:

    Employeepojo匹配数据库表是员工。

    List<Employee> employeeList = sql.select(Tables.Employee)
                                     .from(Tables.EMPLOYEE).fetchInto(Employee.class);
    

    同样,我们如何转换使用连接获取的记录?

    例如:

    Customerpojo匹配数据库表为customer

    Employeepojo匹配数据库表为employee

    sql.select(<<IWantAllFields>>).from(Tables.CUSTOMER)
                                  .join(Tables.EMPLOYEE)
                                  .on(Tables.EMPLOYEE.ID.equal(Tables.CUSTOMER.EMPLOYEE_ID))
                                  .fetchInto(?);
    

    【讨论】:

    【解决方案2】:

    当您想要获取生成的记录类型时,您不应该使用select().from(...) 语法。请改用selectFrom()。这记录在这里:

    http://www.jooq.org/doc/3.1/manual/sql-execution/fetching/record-vs-tablerecord

    所以你的查询应该是:

    Result<CountryRecord> countryRecords = sql.selectFrom(Tables.COUNTRY).fetch();
    for (CountryRecord cr : countryRecords) {
        cr.getNamet();
        //etc...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2022-01-22
      • 2014-01-09
      • 2015-12-30
      • 2019-09-19
      • 2017-01-19
      • 2020-06-12
      • 1970-01-01
      • 2019-03-26
      相关资源
      最近更新 更多