树状图递归

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.List;

@Data
@TableName("person")
public class Person {
    private String id;
    private String name;
    private String parentId;
    //层级
    private Integer level;
    //排序
    private Integer sort;
    //下级
    @TableField(exist = false)
    private List children;
}

老方法递归:

@RestController
@RequestMapping("/person")
public class PersonController2 {

    @Autowired
    private PersonService personService;

    @ApiOperation(value = "获取菜单")
    @GetMapping("queryMenu2")
    public R queryAllMenu() {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        List menus = personService.list(wrapper);
        List list = menus.stream().filter(menu -> "-1".equals(menu.getParentId())).map(
                //设置子节点信息
                menu -> {
                    menu.setChildren(getChildrens(menu, menus));
                    return menu;
           }
        ).sorted(Comparator.comparingInt(Person::getSort)).collect(Collectors.toList());
        return R.ok().data("list",list);
    }

    private List getChildrens(Person root, List menus) {
        List list = menus.stream().filter(menu ->
                //筛选出下一节点元素
                Objects.equals(menu.getParentId(), root.getId())).map(menu -> {
            //递归set子节点
            menu.setChildren(this.getChildrens(menu, menus));
            return menu;
        }).collect(Collectors.toList());
        return list;
    }



    /**
     * @param deps 所有数据
     *//*
    public static void recursive(List deps) {
        init(deps);
        //获取父节点
        List collect = deps.stream()
                .filter(m -> m.getParentId() == 0)
                .map(m ->
                        {
                            m.setChildItem(getChildrens(m, deps));
                            return m;
                        }
                ).collect(Collectors.toList());

        // 普遍请求下,根节点只会有一个,所以这里取出第一个元素,如果由多个,可根据需求调整,这里仅做测试使用
        System.out.println(JSON.toJSON(collect.get(0)));
    }

    *//**
     * 递归查询子节点
     *
     * @param root 根节点
     * @param all  所有节点
     * @return 根节点信息
     *//*
    private static List getChildrens(LrItem root, List all) {
        List children = all.stream()
                .filter(m -> Objects.equals(m.getParentId(), root.getId()))
                .map(m -> {
                            m.setChildItem(getChildrens(m, all));
                            return m;
                        }
                ).collect(Collectors.toList());
        return children;
    }*/


}

新递归:

   @ApiOperation(value = "获取菜单")
    @GetMapping("queryMenu2")
    public R queryAllMenu() {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        List menus = personService.list(wrapper);
        List list = menus.stream().filter(menu -> "-1".equals(menu.getParentId())).map(
                //设置子节点信息
                menu -> {
                    menu.setChildren(getChildrens(menu, menus));
                    return menu;
           }
        ).sorted(Comparator.comparingInt(Person::getSort)).collect(Collectors.toList());
        return R.ok().data("list",list);
    }

    private List getChildrens(Person root, List menus) {
        List list = menus.stream().filter(menu ->
                //筛选出下一节点元素
                Objects.equals(menu.getParentId(), root.getId())).map(menu -> {
            //递归set子节点
            menu.setChildren(this.getChildrens(menu, menus));
            return menu;
        }).collect(Collectors.toList());
        return list;
    }



    /**
     * @param deps 所有数据
     *//*
    public static void recursive(List deps) {
        init(deps);
        //获取父节点
        List collect = deps.stream()
                .filter(m -> m.getParentId() == 0)
                .map(m ->
                        {
                            m.setChildItem(getChildrens(m, deps));
                            return m;
                        }
                ).collect(Collectors.toList());

        // 普遍请求下,根节点只会有一个,所以这里取出第一个元素,如果由多个,可根据需求调整,这里仅做测试使用
        System.out.println(JSON.toJSON(collect.get(0)));
    }

    *//**
     * 递归查询子节点
     *
     * @param root 根节点
     * @param all  所有节点
     * @return 根节点信息
     *//*
    private static List getChildrens(LrItem root, List all) {
        List children = all.stream()
                .filter(m -> Objects.equals(m.getParentId(), root.getId()))
                .map(m -> {
                            m.setChildItem(getChildrens(m, all));
                            return m;
                        }
                ).collect(Collectors.toList());
        return children;
    }*/

 

 

 

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