数据库取值新思路 发表于 2020-05-12 原先列表查询时,如果关联多张表,我会在sql中关联表进行查询 新思路将不同的表,根据我所需要的字段进行查询,先放在对应的Map中,需要的时候从map中取值即可 123456789101112131415161718192021222324252627282930313233// 封装品牌、分类、供货商查询信息Set<Long> brandIds = Sets.newHashSet();Set<Long> categoryIds = Sets.newHashSet();Set<Long> supplierIds = Sets.newHashSet();Set<Long> skuIds = Sets.newHashSet();pages.getContent().forEach(item -> { brandIds.add(item.getBrandId()); categoryIds.add(item.getCategoryId()); categoryIds.add(item.getSubCategoryId()); supplierIds.add(item.getSupplierId()); skuIds.add(item.getSkuId());});Map<Long, Brand> brandMap = brandService.getBrandMap(brandIds);Map<Long, Category> categoryMap = categoryService.getCategoryMap(categoryIds);Map<Long, Supplier> supplierMap = supplierService.getSupplierMap(supplierIds);Map<Long, Long> stockMap = storeSkuStockService.getSkuStockMap(Constants.YUN_CANG_ID, skuIds);// 组装结果信息 pages.getContent().forEach(item -> { SysStoreSkuDTO dto = new SysStoreSkuDTO(); Supplier supplier = supplierMap.get(item.getSupplierId()); if (supplier != null) { dto.setSupplierName(supplier.getSupplierName()); } Long stock = stockMap.get(item.getSkuId()); dto.setStock(stock == null ? 0 : stock); Brand brand = brandMap.get(item.getBrandId()); dto.setBrandName(brand == null ? "" : brand.getBrandName()); Category category = categoryMap.get(item.getCategoryId()); Category subCategory = categoryMap.get(item.getSubCategoryId()); dto.setCategoryName(category == null ? "" : category.getCategoryName()); dto.setSubCategoryName(subCategory == null ? "" : subCategory.getCategoryName()); dtoList.add(dto);}); 赏个🍗吧 打赏 微信支付 支付宝 本文作者: Keeep 本文链接: http://Keeep.coding.me/blog/数据库取值新思路/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!