递归Stream流方式查询多级树形结构(mp)

public List listWithTree() {
    //1.查出所有的类
    List pmsCategoryEntities = baseMapper.selectList(null);
    //2.组装成父子的树形结构
    //2.1 找到所有的一级分类
    List level1Menus = pmsCategoryEntities.stream().filter((pmsCategoryEntity -> {
        return pmsCategoryEntity.getParentCid() == 0;
    })).map((menu)->{
        menu.setChildren(getChildrens(menu,pmsCategoryEntities));
        return menu;
    }).sorted((menu1,menu2)->{
        return menu1.getSort()-menu2.getSort();
    }).collect(Collectors.toList());

// QueryWrapper wrapper = new QueryWrapper<>();
// wrapper.eq(“parent_cid”,)
return level1Menus;
}

/**
 * 获取某一个菜单的子菜单
 * @return
 */
private List getChildrens(PmsCategoryEntity root,List all){
    List children = all.stream().filter(pmsCategoryEntity -> {
        return pmsCategoryEntity.getParentCid() == root.getCatId();
    }).map(pmsCategoryEntity -> {
        pmsCategoryEntity.setChildren(getChildrens(pmsCategoryEntity,all));
        return pmsCategoryEntity;
    }).sorted((menu1,menu2)->{
        return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
    }).collect(Collectors.toList());
    return children;
}

你可能感兴趣的:(递归Stream流方式查询多级树形结构(mp))