gulimall增加三级分类树形结构

需要在三级分类中添加以下的属性  ---  表示非本表数据


//自定义属性
@JsonInclude(JsonInclude.Include.NON_EMPTY)   //不为空,spring才将这个属性返回
@TableField(exist = false)
private List<CategoryEntity> children;

然后在seversimpl 增加一下代码

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

//递归查找所有菜单的子菜单
private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
    List<CategoryEntity> cildren = all.stream().filter(categoryEntity -> {
        return categoryEntity.getParentCid() == root.getCatId();
    }).map(categoryEntity -> {
        //1.找到子菜单
        categoryEntity.setChildren(getChildrens(categoryEntity, all));
        return categoryEntity;
    }).sorted((menu1, menu2) -> {
        //2.菜单排序
        return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
    }).collect(Collectors.toList());
    return cildren;
}

猜你喜欢

转载自blog.csdn.net/jiangchuan465/article/details/127695785