如何在RecordMapper中使用fetchMap()?

时间:2016-08-21 13:01:54

标签: java sql jooq

我知道我可以获取这样的地图:

this.ctx.select(
        shopSubscription.field(SHOP_SUBSCRIPTION.SHOP_ID),
        shopSubscription.field(SHOP_SUBSCRIPTION.PAYMENT_GATEWAY_SUBSCRIPTION_ID),
        shopSubscription.field(SHOP_SUBSCRIPTION.ADMIN_TOOL_FEATURE_TYPE_ID),
        PAYMENT_GATEWAY_SUBSCRIPTION.SUBSCRIPTION_ID_TOKEN
        )
    .from(PAYMENT_GATEWAY_SUBSCRIPTION)
        .join(shopSubscription)
            .on(PAYMENT_GATEWAY_SUBSCRIPTION.ID.eq(shopSubscription.field(SHOP_SUBSCRIPTION.PAYMENT_GATEWAY_SUBSCRIPTION_ID))
                    .and(PAYMENT_GATEWAY_SUBSCRIPTION.PAYMENT_GATEWAY_TYPE_ID.eq(paymentGatewayType)))
        .fetchMap(PAYMENT_GATEWAY_SUBSCRIPTION.PAYMENT_GATEWAY_TYPE_ID, ShopSubscriptionDTO.class);

但是为了在编译时检测问题,我更喜欢我还可以在此查询中添加RecordMapper

那么有没有办法调用fetchMap(),但也提供RecordMapper

我想的是看起来像这样:

this.ctx.select(
        shopSubscription.field(SHOP_SUBSCRIPTION.SHOP_ID),
        shopSubscription.field(SHOP_SUBSCRIPTION.PAYMENT_GATEWAY_SUBSCRIPTION_ID),
        shopSubscription.field(SHOP_SUBSCRIPTION.ADMIN_TOOL_FEATURE_TYPE_ID),
        PAYMENT_GATEWAY_SUBSCRIPTION.SUBSCRIPTION_ID_TOKEN
        )
    .from(PAYMENT_GATEWAY_SUBSCRIPTION)
        .join(shopSubscription)
            .on(PAYMENT_GATEWAY_SUBSCRIPTION.ID.eq(shopSubscription.field(SHOP_SUBSCRIPTION.PAYMENT_GATEWAY_SUBSCRIPTION_ID))
                    .and(PAYMENT_GATEWAY_SUBSCRIPTION.PAYMENT_GATEWAY_TYPE_ID.eq(paymentGatewayType)))

        // For each record apply the map() function

        .map(new RecordMapper<Record<?>, ShopSubscriptionDTO>() {
            @Override
            public ShopSubscriptionDTO map(Record<?> record) {
                ShopSubscriptionDTO shopSubscriptionDto = new ShopSubscriptionDTO();

                shopSubscriptionDto.setShopId(record.getValue(SHOP_SUBSCRIPTION.SHOP_ID)
                // ...

                return shopSubscriptionDto;
            }
        });

        // Fetch the result into a map where the key is SHOP_SUBSCRIPTION.ADMIN_TOOL_FEATURE_TYPE_ID
        .fetchMap(SHOP_SUBSCRIPTION.ADMIN_TOOL_FEATURE_TYPE_ID);

2 个答案:

答案 0 :(得分:1)

由于fetchMap()有很多不同的实现,我也没有看到fetchMap(Field<K>, RecordMapper<? super R, R>)。因此,只需坚持下去就有助于解决这个问题:

// ...
.fetchMap(ADMIN_TOOL_ADD_ON.ADMIN_TOOL_ADD_ON_TYPE_ID, new RecordMapper<Record, AdminToolAddOnDTO>() {

    @Override
    public AdminToolAddOnDTO map(Record record) {

        AdminToolAddOnDTO dto = new AdminToolAddOnDTO();

        dto.setId(record.getValue(ADMIN_TOOL_ADD_ON.ID));
        dto.setAdminToolFeatureTypeId(record.getValue(ADMIN_TOOL_ADD_ON.ADMIN_TOOL_FEATURE_TYPE_ID));
        dto.setAdminToolAddOnTypeId(record.getValue(ADMIN_TOOL_ADD_ON.ADMIN_TOOL_ADD_ON_TYPE_ID));
        dto.setPrice(record.getValue(ADMIN_TOOL_ADD_ON.PRICE));
        dto.setCountryId(record.getValue(ADMIN_TOOL_ADD_ON.COUNTRY_ID));
        dto.setAddOnIdToken(record.getValue(ADMIN_TOOL_ADD_ON_TYPE.ADD_ON_ID_TOKEN));

        return dto;
    }
});

答案 1 :(得分:1)

java 8或更高版本

.fetchMap(CN_TASKS.AGENTID,
                        r -> new CnTaskMessage(r.getValue(CN_TASKS.CN_TASKID), r.getValue(CN_TASKS.TASK_TYPE),
                                r.getValue(CN_TASKS.STATUS)));