一、查找所有商品
1、service
CategoryServiceImpl.java
/**
* 耗时:http(请求微信api) > 磁盘 > 内存
* mysql(内网+磁盘)
* @return
*/
@Override
public ResponseVo<List<CategoryVo>> selectAll() {
List<Category> categories = categoryMapper.selectAll();
//查出parent_id=0
// for (Category category : categories) {
// if (category.getParentId().equals(ROOT_PARENT_ID)) {
// CategoryVo categoryVo = new CategoryVo();
// BeanUtils.copyProperties(category, categoryVo);
// categoryVoList.add(categoryVo);
// }
// }
//lambda + stream
List<CategoryVo> categoryVoList = categories.stream()
.filter(e -> e.getParentId().equals(ROOT_PARENT_ID))
.map(this::category2CategoryVo)
.sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
.collect(Collectors.toList());
//查询子目录
findSubCategory(categoryVoList, categories);
return ResponseVo.success(categoryVoList);
}
this::category2CategoryVo 等价于 e -> category2CategoryVo(e)
2、循环查找子目录
CategoryServiceImpl.java
private void findSubCategory(List<CategoryVo> categoryVoList, List<Category> categories) {
for (CategoryVo categoryVo : categoryVoList) {
List<CategoryVo> subCategoryVoList = new ArrayList<>();
for (Category category : categories) {
//如果查到内容,设置subCategory, 继续往下查
if (categoryVo.getId().equals(category.getParentId())) {
CategoryVo subCategoryVo = category2CategoryVo(category);
subCategoryVoList.add(subCategoryVo);
}
subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
categoryVo.setSubCategories(subCategoryVoList);
findSubCategory(subCategoryVoList, categories);
}
}
}
3、直接拷贝数据
CategoryServiceImpl.java
private CategoryVo category2CategoryVo(Category category) {
CategoryVo categoryVo = new CategoryVo();
BeanUtils.copyProperties(category, categoryVo);
return categoryVo;
}
二、根据categoryId查找多级商品
1、ProductServiceImpl.java文章来源:https://www.toymoban.com/news/detail-651747.html
@Override
public ResponseVo<PageInfo> list(Integer categoryId, Integer pageNum, Integer pageSize) {
Set<Integer> categoryIdSet = new HashSet<>();
if (categoryId != null) {
// 给categoryIdSet赋值子目categoryId
categoryService.findSubCategoryId(categoryId, categoryIdSet);
categoryIdSet.add(categoryId);
}
PageHelper.startPage(pageNum, pageSize);
List<Product> productList = productMapper.selectByCategoryIdSet(categoryIdSet);
List<ProductVo> productVoList = productList.stream()
.map(e -> {
ProductVo productVo = new ProductVo();
BeanUtils.copyProperties(e, productVo);
return productVo;
})
.collect(Collectors.toList());
PageInfo pageInfo = new PageInfo<>(productList);
pageInfo.setList(productVoList);
return ResponseVo.success(pageInfo);
}
2、CategoryServiceImpl.java文章来源地址https://www.toymoban.com/news/detail-651747.html
@Override
public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
List<Category> categories = categoryMapper.selectAll();
findSubCategoryId(id, resultSet, categories);
}
private void findSubCategoryId(Integer id, Set<Integer> resultSet, List<Category> categories) {
for (Category category : categories) {
if (category.getParentId().equals(id)) {
resultSet.add(category.getId());
findSubCategoryId(category.getId(), resultSet, categories);
}
}
}
到了这里,关于SpringBoot查找多级商品方案【一次sql查询所有数据,循环处理分层逻辑】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!