【发布时间】:2018-01-30 05:33:13
【问题描述】:
我不确定我是否走对了路,但目前我卡住了。 我想要实现的是我想根据其父类别获取项目列表。
ParentCategory --< Category --< Item
而且对 CompletionStage 还不是很熟悉。
所以这里我有 2 个查询。第一个查询是获取 ParentCategory 的列表。然后我将遍历该列表以获取每个 ParentCategory 的项目列表
ParentCategoryDao {
findParentCategoriesByShop(int shopId);
}
ItemDao {
//joined query of item and child category
findItemByChildCategory(final int parentCategoryId)
}
在控制器中:
public CompletionStage<List<ProcessClass>> retrieveItems(final int shopId) {
parentCategoryDao.findParentCategoriesByShop(shopId).thenApplyAsync(parentCategoryStream ->{
ParentCategoryJson parentCategoryJson = new ParentCategoryJson();
for(ParentCategory parentCategory : parentCategoryStream.collect(Collectors.toList())) {
processClassJson.setProcessClassId(parentCategory.getId());
processClassJson.setProcessClassName(processClass.getProcessClass());
itemDao.findItemByChildCategory(parentCategory.getId()).thenApplyAsync(itemStream ->{
// Do operations on forming a list of items
}, ec.current());
//then maybe after is something like
processClassJson.setItemList(itemList);
}
},ec.current())
}
顺便说一句,我正在使用 Play Framework。 非常感谢任何帮助。
【问题讨论】:
-
DAO 接口的声明不完整(缺少返回类型),并且您正在使用未声明的
ProcessClass类型和processClass、processClassJson和itemList变量。你到底有什么问题?您尝试了哪些方法,为什么不起作用? -
其中的所有内容仅用于说明目的。
-
没关系,但您应该将其设为minimal reproducible example,并明确指出您的问题所在。
标签: java java-8 completable-future