美文网首页
递归查询树形结构菜单

递归查询树形结构菜单

作者: 初心myp | 来源:发表于2019-07-25 15:46 被阅读0次

查询树形结构的菜单,并且可以排序

/**
    * <p>Discription: 获取栏目的菜单树结构</p>
    * Created on: 10:24 2019/5/29
    * @author muyuanpei
    */
    @Override
    public List<CmsChannel> getByPid() {
        List<CmsChannel> cmsChannels = cmsChannelMapper.selectByPid(0);
        List<CmsChannel> cmsChannelList = new ArrayList<>();
        for (CmsChannel ch:cmsChannels) {
            CmsChannel channel = this.recursiveTree(ch.getChannelId());
            cmsChannelList.add(channel);
        }
//        Comparator<CmsChannel> comparing = comparing((CmsChannel a) -> a.getPriority());
        cmsChannelList.sort(comparing(CmsChannel::getPriority).thenComparing(CmsChannel::getChannelId));
        return cmsChannelList;
    }

    public CmsChannel recursiveTree(int cid) {
        //根据cid获取节点对象(查询当前节点的对象)
        CmsChannel channel = cmsChannelMapper.queryById(cid);
        //查询cid下的所有子节点(查询当前节点的子节点)
        List<CmsChannel> cmsChannels = cmsChannelMapper.selectByPid(cid);
        cmsChannels.sort(comparing(CmsChannel::getPriority).thenComparing(CmsChannel::getChannelId));
        //遍历子节点
        for(CmsChannel child : cmsChannels){
            CmsChannel ch = recursiveTree(child.getChannelId());//递归
            channel.getChannelList().add(ch);
        }
        return channel;
    }

使用lambda表达式给list排序

/**
 * 正序排序
 */
// 使用Comparator的comparing
Comparator<Apple> comparing = comparing((Apple a) -> a.getWeight());
inventory.sort(comparing((Apple a) -> a.getWeight()));
//或者等价于
inventory.sort(comparing(Apple::getWeight));


/**
 * 逆序排序
 */
// 1、 根据重量逆序排序
inventory.sort(comparing(Apple::getWeight).reversed());  
// 2、如果两个苹果的重量一样重,怎么办?那就再找一个条件进行排序呗
inventory.sort(comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));

参考:https://www.cnblogs.com/linjiqin/p/3148066.html
https://blog.csdn.net/u012587693/article/details/52474282

相关文章

网友评论

      本文标题:递归查询树形结构菜单

      本文链接:https://www.haomeiwen.com/subject/afyetctx.html