一级分类获取

**得到子菜单数据 **

public List<CategoryEntity> listWithTree() {
        //1,查询出所有菜单
        List<CategoryEntity> entities = baseMapper.selectList(null);
        //2,查出所有一级分类
        List<CategoryEntity> list1 = entities.stream().filter(categoryEntity ->
                        categoryEntity.getParentCid() == 0)
                .map(emun -> {
                    emun.setChildren(getChildrens(emun,entities));
                    return emun;
                }).sorted((emun1, emun2) -> {
                return (emun1.getSort()==null ? 0:emun1.getSort()) - (emun2.getSort()==null ?0:emun2.getSort());
                })
                .collect(Collectors.toList());
        return list1;
    }
 //递归查询出所有父类菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity ->{
                    return categoryEntity.getParentCid() == root.getCatId();
                        }).map(categoryEntity -> {
                    //使用递归进行查出所有父菜单的所有子菜单
                    categoryEntity.setChildren(getChildrens(categoryEntity, all));
                    return categoryEntity;
                }).sorted((emun1, emun2) -> {
                    //对同级菜单进行排序
                   return (emun1.getSort()==null ?0:emun1.getSort()) - (emun2.getSort()==null ?0:emun2.getSort());
                })
                .collect(Collectors.toList());

        return children;
    }

你可能感兴趣的:(SpringCloud,java,开发语言)