for循环中,进行SQL查询,提取出来先查询

开发中,常见有for循环里面有SQL操作,如果数据量倒也无妨,数据量一旦大就容易性能很差,故记录下优化方式

InfoType infoType = handleDataMap(orderPage.getDatas());
for(Order order : orderPage.getDatas()) {
            ReportViewResp resp = new ReportViewResp();
            BeanUtils.copyProperties(order, resp);
            createReportInfo(infoType,resp, order);
            if (ObjectUtil.isNotNull(testType)){
                //如果测试类型为:元器件,状态为:报告制作,那么把测试结果设置为null
                if (testType.getId().equals(order.getTestTypeId())
                        && (OrderStatusEnum.REPORT_MAKE.getCode().equals(order.getOrderStatus())
                        || OrderStatusEnum.REPORT_REJECT.getCode().equals(order.getOrderStatus()))){
                    resp.setResult(null);
                }
            }
            result.add(resp);
        }
private InfoType handleDataMap(List<Order> orderList) {
        if (CollUtil.isEmpty(orderList)){
            return null;
        }
        InfoType infoType = new InfoType();
        // TestType testType = testTypeCommand.getById(order.getTestTypeId());
        List<Long> testTypeIds = orderList.stream().map(Order::getTestTypeId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<TestType> testTypeList = testTypeCommand.list(new LambdaQueryWrapper<TestType>()
                .in(!testTypeIds.isEmpty(),TestType::getId, testTypeIds).or().apply(testTypeIds.isEmpty(),"1=0") );
        Map<Long, TestType> testTypeMap = testTypeList.stream().collect(Collectors.toMap(TestType::getId, TestType -> TestType));
        infoType.setTestTypeMap(testTypeMap);
// Lab lab = labCommand.getById(order.getLabId());
        List<Long> labIds = orderList.stream().map(Order::getLabId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<Lab> labList = labCommand.list(new LambdaQueryWrapper<Lab>()
               .in(!labIds.isEmpty(),Lab::getId, labIds).or().apply(labIds.isEmpty(),"1=0").select(Lab::getId,Lab::getLabName) );
        Map<Long, Lab> labMap = labList.stream().collect(Collectors.toMap(Lab::getId, Lab -> Lab));
        infoType.setLabMap(labMap);
        // Sample sample = sampleCommand.getById(order.getSampleId());
        List<Long> sampleIds = orderList.stream().map(Order::getSampleId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<Sample> sampleList = sampleCommand.list(new LambdaQueryWrapper<Sample>()
                .in(!sampleIds.isEmpty(),Sample::getId, sampleIds).or().apply(sampleIds.isEmpty(),"1=0").select(Sample::getId,Sample::getSampleName) );
         Map<Long, Sample> sampleMap = sampleList.stream().collect(Collectors.toMap(Sample::getId, Sample -> Sample));
        infoType.setSampleMap(sampleMap);
        // Brand brand = brandCommand.getById(order.getBrandId());
        List<Long> brandIds = orderList.stream().map(Order::getBrandId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<Brand> brandList = brandCommand.list(new LambdaQueryWrapper<Brand>()
                .in(!brandIds.isEmpty(),Brand::getId, brandIds).or().apply(brandIds.isEmpty(),"1=0").select(Brand::getId,Brand::getBrandName) );
        Map<Long, Brand> brandMap = brandList.stream().collect(Collectors.toMap(Brand::getId, Brand -> Brand));
        infoType.setBrandMap(brandMap);

 

@Data
public class InfoType {

    private Map<Long, TestType> testTypeMap = new HashMap<>();

    private Map<Long, Lab> labMap = new HashMap<>();

    private Map<Long, Sample> sampleMap = new HashMap<>();

    private Map<Long, Brand> brandMap = new HashMap<>();

    private Map<Long, Customer> customerMap = new HashMap<>();

    private Map<Long, Supplier> supplierMap = new HashMap<>();

    private Map<String, BaseData> baseDataMap = new HashMap<>();

    private Map<String, ReceiveSample> receiveSampleMap = new HashMap<>();


}

 

private void createReportInfo(InfoType infoType,ReportViewResp resp, Order order) {

        if (order.getOrderStatus() != null && order.getOrderStatus() > OrderStatusEnum.TEST_EXC.getCode()) {
            if (order.getTestResult() != null){
                resp.setResult(ResultEnum.codeOf(order.getTestResult()));
            }
        }
     //TestType testType = testTypeCommand.getById(order.getTestTypeId());
        Map<Long, TestType> testTypeMap = infoType.getTestTypeMap();
        TestType testType = testTypeMap.get(order.getTestTypeId());
        //Lab lab = labCommand.getById(order.getLabId());
        Map<Long, Lab> labMap = infoType.getLabMap();
        Lab lab = labMap.get(order.getLabId());
        if (Objects.nonNull(lab)) {
            resp.setLab(lab.getLabName());
        }
        //Sample sample = sampleCommand.getById(order.getSampleId());
        Map<Long, Sample> sampleMap = infoType.getSampleMap();
        Sample sample = sampleMap.get(order.getSampleId());
        if (Objects.nonNull(sample)) {
            resp.setSample(sample.getSampleName());
        }
        //Brand brand = brandCommand.getById(order.getBrandId());
        Map<Long, Brand> brandMap = infoType.getBrandMap();
        Brand brand = brandMap.get(order.getBrandId());
        if (Objects.nonNull(brand)) {
            resp.setBrand(brand.getBrandName());
        }    

其中in的时候,有可能会集合为null,所以处理下用or(),这样就不会报错了

 

posted @ 2026-05-15 10:30  多多指教~  阅读(20)  评论(0)    收藏  举报