简体   繁体   English

Jooq Result的自定义转换器

[英]Custom converter for Jooq Result

Actually we are using fetchInto() to convert the results to list of object. 实际上我们使用fetchInto()将结果转换为对象列表。

For example: 例如:

Employee pojo matching database table is employee. Employee pojo匹配数据库表是员工。

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

similarly, how could we convert the records we are fetching using joins? 同样,我们如何转换我们使用连接获取的记录?

For example: 例如:

Customer pojo matching database table is customer . Customer pojo匹配数据库表是customer

Employee pojo matching database table is employee . Employee pojo匹配数据库表是employee

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

To select all fields from a joined table source, simply select "none": 要从连接的表源中选择所有字段,只需选择“none”:

Result<Record> result =
sql.select().from(Tables.CUSTOMER)
            .join(Tables.EMPLOYEE)
            .on(...)
            .fetch();

jOOQ will then introspect the known table source and generate all column references for you. 然后,jOOQ将对已知的表源进行内省并为您生成所有列引用。 One way to create a POJO relationship is to use one of the various Result.intoGroups() methods. 创建POJO关系的一种方法是使用各种Result.intoGroups()方法之一。 Eg: 例如:

Map<Integer, List<Customer>> map =
result.intoGroups(CUSTOMER.EMPLOYEE_ID, Customer.class);

This will produce a map of List<Customer> pojos per EMPLOYEE_ID value. 这将生成每个EMPLOYEE_ID值的List<Customer> pojos的映射。

On a side-note: As with any mapping operation that calls upon the DefaultRecordMapper , mapping might not work as expected when your JOIN operation produces two times the same column name (eg CUSTOMER.ID and EMPLOYEE.ID ) - as the DefaultRecordMapper doesn't know what table a particular column originates from. 旁注:与调用DefaultRecordMapper任何映射操作一样,当JOIN操作产生两次相同的列名时,映射可能无法按预期工作(例如CUSTOMER.IDEMPLOYEE.ID ) - 因为DefaultRecordMapper没有'知道特定列来自哪个表。

For more sophisticated mapping, you should probably implement your own RecordMapperProvider 对于更复杂的映射,您应该实现自己的RecordMapperProvider

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

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