首页 > 技术文章 > 谷粒商城-三级分类

fayeelse 2021-09-28 12:58 原文

三级分类样式:

 

首先,在CategoryController中的list()方法里面自己创建一个方法listWithTree(),此方法可以查出所有分类及子分类,并按照树形结构组装起来。在Service和Impl中生成此方法。

 

 

 在实现类CategorySrviceImpl里面编写主要逻辑

 

 

首先查找出所有分类

List<CategoryEntity> entities = baseMapper.selectList(null);

用stream流找出所有一级分类,条件为所有的parentCid均为0(一级菜单的id均为0),用steam流提供的映射方法map()重新改变菜单,也就是说在map里使用递归的方法getChildrens()把当前的一级菜单和所有数据作为参数传递,依次找出二级菜单和三级菜单。

map((menu)->{
menu.setChildren(getChildrens(menu,entities));
return menu;
}

递归方法getChildrens:

 

 

与listWithTree方法类似,在全部数据里面找出ParentCid和一级菜单中CatId相等的数据,因为一共有多级菜单,所以可以采用递归的方法。

 

map(categoryEntity -> {
//找到子菜单
categoryEntity.setChildren(getChildrens(categoryEntity,all));
return categoryEntity;
}

 

依旧是把当前菜单也就是二级菜单和全部数据作为参数传递给递归的方法。


 

推荐阅读