springboot+mybatis查询及组装树结构

概述

在实际开发中经常遇到需要查询树结构的需求。有两种方法,一种是递归查询子集,另一种是用Map映射子集。

递归

首先找到所有的一级主题,即没有父节点的类别。
然后通过stream.Map遍历处理每一个主题,
然后每一个一级主题都设置其子主题:
topicTree.setChildren(findChildren2(topicTree, topicTreeList))
findChildren2就是找到子主题的方法

@Override
public List<TopicTree> queryTopicTree2() {
     List<TopicTree> topicTreeList = this.list(Wrappers.<TopicTree>lambdaQuery().orderByDesc(TopicTree::getOrders));
     List<TopicTree> list = topicTreeList.stream()
             .filter(topicTree -> Objects.isNull(topicTree.getPid()))
             .map(topicTree -> {
                 topicTree.setChildren(findChildren2(topicTree, topicTreeList));
                 return topicTree;
             })
             .collect(Collectors.toList());
     return list;
 }

输入的参数有两个,一个是当前的主题opicTree,另一个是所有主题的列表。
找到子主题的关键在于,找到当前主题的id=子主题的pid。
过滤之后,用map处理每个子主题,而子主题的子主题则递归调用findChirdren2方法查找。

    private List<TopicTree> findChildren2(TopicTree topicTree, List<TopicTree> topicTreeList) {
        List<TopicTree> list = topicTreeList.stream()
                .filter(t -> Objects.nonNull(t.getPid()) && Objects.equals(t.getPid(), topicTree.getId()))
                .map(t -> {
                    t.setChildren(findChildren2(t, topicTreeList));
                    return t;
                })
                .collect(Collectors.toList());
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

Map

首先找到所有的主题列表。
然后用groupingBy方法分组。
这种方式的关键点在于理解groupingBy方法。
Optional.ofNullable(t.getPid()))表示:
如果t.getPid()为null,则返回一个Option.Empty():List的键值对。
如果t.getPid()不为null,则返回Pid:List的键值对。
所以接下来遍历map中每一个键值,而值又是一个list,所以再遍历list,找到当前主题的id,然后在map中找pid=id的键值对,该键值对的值就是子主题。

@Override
    public List<TopicTree> queryTopicTree3() {
        List<TopicTree> topicTreeList = this.list(Wrappers.<TopicTree>lambdaQuery().orderByDesc(TopicTree::getOrders));
        Map<Optional<Long>, List<TopicTree>> topicTreeMap = topicTreeList.stream()
        .collect(Collectors.groupingBy(t -> Optional.ofNullable(t.getPid())));
        topicTreeMap.forEach((pid, topicTrees)->{
            topicTrees.forEach(topicTree -> {
                Long id = topicTree.getId();
                if(Objects.isNull(id)){
                    return;
                }
                List<TopicTree> children = topicTreeMap.get(Optional.of(id));
                if(!CollectionUtils.isEmpty(children)){
                    topicTree.setChildren(children);
                }
            });
        });
        List<TopicTree> res = topicTreeMap.get(Optional.empty());
        if(!CollectionUtils.isEmpty(res)){
            return res;
        }
        return res;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值