springboot接口提高查询速度方法
·
接口想要提高查询速度,需要减少查询数据库的次数,需要把循环里面的查询提出来一次性查询完毕,然后通过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);
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)