我是JOOQ的新手。
而且我需要使用SelectQuery并返回Result,但是它的返回类型是Record。是否可以在Record5中使用selectQuery?还是有其他解决方案?
SelectQuery<Record> query = db.selectQuery();
query.addSelect(WARE.NAME, RECEIPTITEM.SALEPRICE, RECEIPTITEM.QUANTITY, RECEIPT.TSCCREATED, WARE.WARECATEGORYID);
query.addFrom(RECEIPT
.join(RECEIPTITEM).on(RECEIPT.ID.eq(RECEIPTITEM.RECEIPTID))
.join(OUTLET).on(OUTLET.ID.eq(RECEIPT.OUTLETID))
.join(WARE).on(WARE.ID.eq(RECEIPTITEM.WAREID)));
query.addConditions(RECEIPT.TSCCREATED.between(start, end)
.and(OUTLET.OWNERID.eq(ownerId)));
if (outletId != null) {
query.addConditions(RECEIPT.OUTLETID.eq(outletId));
}
if (merchantId != null) {
query.addConditions(RECEIPT.MERCHID.eq(merchantId));
}
最佳答案
您不能使用jOOQ创建SelectQuery<Record5<...>>
类型。
原因是SelectQuery
(which is part of the model API)允许您继续通过SelectQuery.addSelect()
向其添加新列(这是您自己完成的)。每个此类调用都会使Record5<...>
类型无效。
但是,如果动态部分只是附加条件,则不必使用模型API来构造动态SQL。您可以这样重写查询:
Condition condition = RECEIPT.TSCCREATED.between(start, end)
.and(OUTLET.OWNERID.eq(ownerId));
if (outletId != null) {
condition = condition.and(RECEIPT.OUTLETID.eq(outletId));
}
if (merchantId != null) {
condition = condition.and(RECEIPT.MERCHID.eq(merchantId));
}
Select<Record5<String, BigDecimal, Integer, Timestamp, Long>> select =
db.select(
WARE.NAME,
RECEIPTITEM.SALEPRICE,
RECEIPTITEM.QUANTITY,
RECEIPT.TSCCREATED,
WARE.WARECATEGORYID)
.from(RECEIPT
.join(RECEIPTITEM).on(RECEIPT.ID.eq(RECEIPTITEM.RECEIPTID))
.join(OUTLET).on(OUTLET.ID.eq(RECEIPT.OUTLETID))
.join(WARE).on(WARE.ID.eq(RECEIPTITEM.WAREID)))
.where(condition);
关于java - JOOQ selectQuery for Record5,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32453234/