数据库取值新思路

原先

列表查询时,如果关联多张表,我会在sql中关联表进行查询

新思路

将不同的表,根据我所需要的字段进行查询,先放在对应的Map中,需要的时候从map中取值即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 封装品牌、分类、供货商查询信息
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);
});
赏个🍗吧
0%