【问题标题】:How to create jooq custom record?如何创建jooq自定义记录?
【发布时间】:2018-09-12 18:00:20
【问题描述】:

如何从现有的两个其他记录创建自定义 jOOQ 记录,即合并两个现有记录对象的属性。

例如:

CustomerRecord(id, name, surname), 
ProductRecord(id, customer_id, description)
SELECT * FROM Customer JOIN Product ON Customer.id = Product.customer_id;

在这样的查询之后,我将获得 RecordImpl 对象,并且我想要自定义一个可以访问两个表中的字段属性的对象。

【问题讨论】:

    标签: java sql jooq


    【解决方案1】:

    有多种选择可以实现您的目标。

    使用视图

    其中之一是简单地创建一个视图:

    -- Potentially disambiguate the ID (and other) columns
    CREATE VIEW CustomerAndProducts AS
    SELECT * FROM Customer JOIN Product ON Customer.id = Product.customer_id;
    

    然后代码生成器会选择该视图并为您生成一个CustomerAndProductsRecord

    使用你自己的CustomRecord

    您可以通过扩展 org.jooq.impl.CustomRecord 创建自己的 TableRecord 子类型,如下所述: https://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-queryparts

    它们的工作方式几乎与普通的 TableRecord 类型一样,并且可以拥有自己的一组 getter / setter

    使用任何你喜欢的课程

    您不必使用记录。 jOOQ 可以使用以下代码将您的结果提取到任何类型的类(可能是记录,见上文):

    List<MyClass> list =
    DSL.using(configuration)
       .select()
       .from(CUSTOMER)
       .join(PRODUCT)
       .on(CUSTOMER.ID.eq(PRODUCT.CUSTOMER_ID))
       .fetchInto(MyClass.class);
    

    在上面的例子中,DefaultRecordMapper 被应用于 jOOQ 的记录和你的类之间的映射。 See its Javadoc for details.

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2018-01-09
      • 2017-12-23
      • 1970-01-01
      • 2019-02-09
      • 2018-12-30
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多