我可以返回每个值的键列表以将记录映射到地图中吗?

时间:2016-03-22 00:28:14

标签: java sql jooq

我不确定标题,但我想要的是以下内容:

表格shop_offer_time_period说明要约的有效期。这是由valid_from_dayvalid_until_day以及day_of_week_id完成的。

这样的时间段总是有效n * 7天。这意味着,如果我从2000-01-01提取到3000-01-01我可能会多次shop_offer shop_offer_time_period,如果// .. "2015-01-05": [{"offer_id":1}], "2015-01-06": [{"offer_id":2}, {"offer_id":3}], "2015-01-07": [], "2015-01-08": [], "2015-01-09": [], "2015-01-10": [], "2015-01-11": [], "2015-01-12": [{"offer_id":1}], "2015-01-13": [{"offer_id":2}, {"offer_id":3}], // .. 说例如每个星期一提议有效 2015-01-01 2018-01-01

这就是为什么我想在像这样的地图中获取它

RecordMapper

我想知道我是否可以提供一个fetchInto来返回单个记录的密钥列表。

以下是我的fetch目前的示例。目前我正在接受Table<?> asTable = this.ctx.select( SHOP_OFFER_TIME_PERIOD.SHOP_OFFER_ID, SHOP_OFFER_TIME_PERIOD.PRICE ) .from(SHOP_OFFER_TIME_PERIOD) .where( SHOP_OFFER_TIME_PERIOD.SHOP_ID.eq(shopId) .and( // Contained SHOP_OFFER_TIME_PERIOD.VALID_FROM_DAY.greaterOrEqual(fromDay) .and(SHOP_OFFER_TIME_PERIOD.VALID_UNTIL_DAY.lessOrEqual(toDay)) // Overlapping from left .or(SHOP_OFFER_TIME_PERIOD.VALID_FROM_DAY.lt(fromDay) .and(SHOP_OFFER_TIME_PERIOD.VALID_UNTIL_DAY.gt(fromDay)) // Overlapping from right .or(SHOP_OFFER_TIME_PERIOD.VALID_FROM_DAY.lt(toDay) .and(SHOP_OFFER_TIME_PERIOD.VALID_UNTIL_DAY.gt(toDay)))))) .asTable("shopOfferTimePeriod"); List<ShopOfferDTO> fetchInto = this.ctx.select( SHOP_OFFER.ID, SHOP_OFFER.SHOP_ID, SHOP_OFFER.SHOP_TIMES_TYPE_ID, asTable.field(SHOP_OFFER_TIME_PERIOD.DAY_OF_WEEK_ID), asTable.field(SHOP_OFFER_TIME_PERIOD.PRICE) ) .from(SHOP_OFFER) .join(asTable) .on(asTable.field(SHOP_OFFER_TIME_PERIOD.SHOP_OFFER_ID).eq(SHOP_OFFER.ID) .and(SHOP_OFFER.SHOP_TIMES_TYPE_ID.eq(offerType))) .fetchInto(ShopOfferDTO.class); 并在其他地方进行映射。但是,如果它在某种程度上可能,我想在我的存储库中进行映射。

import scipy.special as special
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
import scipy.special as special
 File "C:\DevTools\Python35\lib\site-packages\scipy\special\__init__.py", line 629, in <module>
from .basic import *
File "C:\DevTools\Python35\lib\site-packages\scipy\special\basic.py", line 14, in <module>
from ._ufuncs import (ellipkm1, mathieu_a, mathieu_b, iv, jv, gamma, psi, zeta,
ImportError: cannot import name 'ellipkm1'

请注意,我已经将结果提取到我的DTO而不是生成的记录对象。

1 个答案:

答案 0 :(得分:0)

有很多方法可以解答这个问题。也许,你正在寻找一个SQL解决方案,但这个问题目前的措辞方式,这个答案可能会这样做。

最简单的方法是在从数据库中获取数据后,为您提供在Java中创建内存中分组的两种方法:

使用jOOQ API:

Map<Date, List<Integer>> result =
this.ctx.select(
    ...
)
.from(SHOP_OFFER).join(...)
.fetchGroups(SHOP_OFFER.DATE_COLUMN, SHOP_OFFER.ID);

(我假设你这里有DATE_COLUMN

使用Java 8

jOOQ与Java 8的Stream API(examples can be seen in this blog post)无缝集成,因此解决问题的最有效方法可能是使用Java 8 Streams:

Map<Date, List<Integer>> result =
this.ctx.select(
    ...
)
.from(SHOP_OFFER).join(...)
.fetch()
.stream()
.collect(
    Collectors.groupingBy(
        r -> r.getValue(SHOP_OFFER.DATE_COLUMN),
        Collectors.mapping(
            r -> r.getValue(SHOP_OFFER.ID),
            Collectors.toList()
        )
    )
)