接口想要提高查询速度,需要减少查询数据库的次数,需要把循环里面的查询提出来一次性查询完毕,然后通过java代码来获取响应的值。如下所示:
List<OrderInfoHtVO> orderInfoList = orderInfoService.getOrderInfoHtlist(query);
if(CollectionUtils.isNotEmpty(orderInfoList)){
List<Integer> orderIdList = orderInfoList.stream().map(OrderInfoHtVO::getOrderId)
.collect(Collectors.toList());
List<String> orderNoList = orderInfoList.stream().map(OrderInfoHtVO::getOrderNumber)
.collect(Collectors.toList());
List<InvoiceStatusVO> invoiceStatusVOList = applayInvoiceInfoService.
getInvoiceStatusByOrderList(orderIdList,orderNoList,orderInfoList);
Map<Integer,List<InvoiceStatusVO>> invoiceStatusVOMap = invoiceStatusVOList.stream().
collect(Collectors.groupingBy(InvoiceStatusVO::getOrderId));
List<ApplayInvoiceHongChongCountVO> applayInvoiceHongChongCountVOList = applayInvoiceInfoService
.searchApplayInvoiceHongChongCountListByOrderNoList(orderNoList);
Map<String,List<ApplayInvoiceHongChongCountVO>> applayInvoiceHongChongCountVOMap = applayInvoiceHongChongCountVOList.stream()
.collect(Collectors.groupingBy(ApplayInvoiceHongChongCountVO::getOrderNo));
List<PackageGoodsCountVO> packageGoodsCountVOList = orderInfoService
.searchPackageGoodsCountListByOrderIdList(orderIdList);
Map<Integer,List<PackageGoodsCountVO>> packageGoodsCountVOMap = packageGoodsCountVOList.stream()
.collect(Collectors.groupingBy(PackageGoodsCountVO::getOrderId));
for (OrderInfoHtVO orderInfoHtVO : orderInfoList) {
if(CollectionUtils.isNotEmpty(invoiceStatusVOMap.get(orderInfoHtVO.getOrderId()))){
orderInfoHtVO.setInvoiceStatus(
invoiceStatusVOMap.get(orderInfoHtVO.getOrderId()).get(0).getInvoiceStatus());
}else{
orderInfoHtVO.setInvoiceStatus(0);
}
if(CollectionUtils.isNotEmpty(applayInvoiceHongChongCountVOMap.get(orderInfoHtVO
.getOrderNumber()))){
orderInfoHtVO.setIsHongChong(applayInvoiceHongChongCountVOMap
.get(orderInfoHtVO.getOrderNumber()).get(0).getIsHongChong());
}else{
orderInfoHtVO.setIsHongChong(0);
}
if(CollectionUtils.isNotEmpty(packageGoodsCountVOMap.get(orderInfoHtVO.getOrderId()))){
orderInfoHtVO.setIsPackage(packageGoodsCountVOMap
.get(orderInfoHtVO.getOrderId()).get(0).getIsPackage());
}else{
orderInfoHtVO.setIsPackage(0);
}
}
}
提高接口性能:JavaStreamAPI批量数据库查询优化
本文介绍了如何通过将循环中的数据库查询提取并利用JavaStreamAPI一次性执行,以提高接口查询速度。示例中展示了如何收集订单信息、发票状态、应用红包统计和包裹商品计数的数据并更新到OrderInfoHtVO对象。
995

被折叠的 条评论
为什么被折叠?



