58、商城业务---首页---渲染二级三级目录
类似于下面这种,当我们鼠标悬浮在一级分类上时,会查询该一级分类对应的二级分类和三级分类

(前端如何实现的不用考虑,下面是后端查询分类的代码逻辑)

要求我们返回的数据是json格式,如下:
11是一级分类id,然后11下面是二级分类,二级分类的vo包括一级分类的id,三级分类的list,二级分类本身的id和name;
二级分类下的三级分类包括二级分类的id,三级分类本身的id和name
后端返回Map,即Json对象格式,如上图所示的格式
逻辑如下:
1、vo层

2、controller层

3、serviceImpl层
@Override
    public Map<String, List<CatelogTwoLevelVo>> getCatalogJson() {
        //查询所有的一级分类
        List<CategoryEntity> categoryEntities = this.selectOneLevelCategory();
        //查询所有的二级分类和三级分类并封装数据
        //map的key是一级分类的id,v是二级分类的vo
        Map<String, List<CatelogTwoLevelVo>> map = categoryEntities.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
            //查询每一个一级分类下的二级分类
            List<CategoryEntity> categoryTwoLevelEntities = this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", v.getCatId()));
            List<CatelogTwoLevelVo> collect = null;
            if (categoryTwoLevelEntities!=null){
                collect = categoryTwoLevelEntities.stream().map(l2 -> {
                    //查询该二级分类对应的三级分类并封装数据
                    List<CategoryEntity> categoryThreeLevelEntities = this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", l2.getCatId()));
                    List<CatelogTwoLevelVo.CatalogThreeLevelVo> catalogThreeLevelVos = null;
                    if (categoryThreeLevelEntities!=null){
                        catalogThreeLevelVos = categoryThreeLevelEntities.stream().map(l3 -> {
                            CatelogTwoLevelVo.CatalogThreeLevelVo catalogThreeLevelVo = new CatelogTwoLevelVo.CatalogThreeLevelVo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());
                            return catalogThreeLevelVo;
                        }).collect(Collectors.toList());
                    }
                    //构造二级分类的vo
                    CatelogTwoLevelVo catelogTwoLevelVo = new CatelogTwoLevelVo(v.getCatId().toString(), catalogThreeLevelVos, l2.getCatId().toString(), l2.getName());
                    return catelogTwoLevelVo;
                }).collect(Collectors.toList());
            }
            return collect;
        }));
        return map;
    }
                    
                
                
            
        
浙公网安备 33010602011771号