获取菜单树状结构以及根据名字在树状结构做模糊查询

获取菜单树状结构

菜单实体:

public class Menu implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("name")
    private String name;

    @TableField("parent_id")
    private String parentId;

    @TableField("url")
    private String url;

    @TableField("icon")
    private String icon;

    @TableField("order_num")
    private String orderNum;

    @TableField(exist = false)
    private List<Menu> children;
}

数据结构:
在这里插入图片描述
获取菜单树状结构:

// 通过递归查询获取菜单树状结构
    public List<Menu> getMenuTree(){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id","0");
        List<Menu> menuList = mapper.selectList(queryWrapper);
        commonMethod(menuList);
        return menuList;
    }

    public void commonMethod(List<Menu> menuList){
        for(Menu menu : menuList){
            Integer id = menu.getId();
            QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            List<Menu> menuList2 = mapper.selectList(queryWrapper);
            menu.setChildren(menuList2);
            commonMethod(menuList2);
        }
    }

获取菜单结果:

[
    {
        "id": 1,
        "name": "Java",
        "parentId": "0",
        "url": "http://www.aliouchen.com",
        "icon": "a",
        "orderNum": "1",
        "children": [
            {
                "id": 2,
                "name": "并发编程",
                "parentId": "1",
                "url": "http://www.aliouchen.com",
                "icon": "a",
                "orderNum": "1",
                "children": []
            },
            {
                "id": 3,
                "name": "多线程",
                "parentId": "1",
                "url": "http://www.aliouchen.com",
                "icon": "a",
                "orderNum": "2",
                "children": [
                    {
                        "id": 4,
                        "name": "Thread",
                        "parentId": "3",
                        "url": "http://www.aliouchen.com",
                        "icon": "a",
                        "orderNum": "1",
                        "children": []
                    }
                ]
            }
        ]
    },
    {
        "id": 5,
        "name": "Python",
        "parentId": "0",
        "url": "http://www.aliouchen.com",
        "icon": "a",
        "orderNum": "2",
        "children": [
            {
                "id": 6,
                "name": "Tuple",
                "parentId": "5",
                "url": "http://www.aliouchen.com",
                "icon": "a",
                "orderNum": "1",
                "children": []
            }
        ]
    }
]

根据名字在树状结构做模糊查询

根据查询名字获取菜单树状结构:

// 通过查询名字使用递归方法获取菜单树状结构
    //在树结构上做模糊查询(剪枝操作)
    @Override
    public List<Menu> getMenuByName(List<Menu> menuList,String selectName){
        Iterator<Menu> iterator = menuList.iterator();
        while(iterator.hasNext()){
            Menu menu = iterator.next();
            if(!menu.getName().contains(selectName)){
                List<Menu> childrenList = menu.getChildren();
                if(!CollectionUtils.isEmpty(childrenList)){
                    getMenuByName(childrenList, selectName);
                }
                if(CollectionUtils.isEmpty(childrenList)){
                    iterator.remove();
                }
            }
        }
        return menuList;
    }

例如查询名字"多线程"结果:

[
    {
        "id": 1,
        "name": "Java",
        "parentId": "0",
        "url": "http://www.aliouchen.com",
        "icon": "a",
        "orderNum": "1",
        "children": [
            {
                "id": 3,
                "name": "多线程",
                "parentId": "1",
                "url": "http://www.aliouchen.com",
                "icon": "a",
                "orderNum": "2",
                "children": [
                    {
                        "id": 4,
                        "name": "Thread",
                        "parentId": "3",
                        "url": "http://www.aliouchen.com",
                        "icon": "a",
                        "orderNum": "1",
                        "children": []
                    }
                ]
            }
        ]
    }
]
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值