[项目练手笔记-谷粒商城(SpringCloud Alibaba+vue前后端分离)]day03-day07三级分类API
一、获取三级分类树形列表
一、controller调用
/**
* 查询所有产品,并以属性列表展示
*/
@RequestMapping("/list/tree")
//@RequiresPermissions("product:category:list")
public R list(){
List<CategoryEntity> entities=categoryService.listWithTree();
return R.ok().put("data", entities);
}
二、递归查找子节点
1.修改entity
/**
* 所有子节点
*/
@TableField(exist=false)
private List<CategoryEntity> children;
2.递归查找子节点
public List<CategoryEntity> listWithTree() {
//1.查询所有产品信息
List<CategoryEntity> list = baseMapper.selectList(null);
//TODO 2.查询所有子菜单信息
List<CategoryEntity> level1Menu = list.stream().
filter(categoryEntity ->
//找到所有一级菜单
categoryEntity.getParentCid() == 0).
map((menu1) -> {
//给所有的一级菜单找到他们的子节点
menu1.setChildren(getchildren(menu1, list));
return menu1;
}).
sorted(
//排序菜单
Comparator.comparingInt(CategoryEntity::getSort)).
collect(
//收集所有一级菜单信息
Collectors.toList());
return level1Menu;
}
private List<CategoryEntity> getchildren(CategoryEntity menu1, List<CategoryEntity> list) {
List<CategoryEntity> children = list.stream().
filter(
categoryEntity -> categoryEntity.getParentCid().equals(menu1.getCatId())).
map((menu) -> {
menu.setChildren(getchildren(menu, list));
return menu;
}).
sorted(
//排序菜单
Comparator.comparingInt(menuOne -> (menuOne.getSort() == null ? 0 : menuOne.getSort()))).
collect(
//收集所有一级菜单信息
Collectors.toList());
return children;
}
三、前端编写
进入文件,首选项,用户片段配置vue模板
vue全局模板
{
"vue全局模板": {
"prefix":