递归树:获取父目录下的所有子目录

目录结构图:

实体结构:

private Long dirId;
private Long dirParentId;

需求背景:

我需要点击父级目录时查询所有该目录的数据+所有该目录下的所有子目录的数据(子目录有无穷多层)

调用方:

List<ZmgrDataColumnDir> zmgrDataColumnDirList = Lists.newArrayList();
List<ZmgrDataColumnDir> dirIds = dataColumnService.selectByParentDirId(zmgrDataColumnDirList, dirId);
List<Long> dirIdList = dirIds.stream().map(ZmgrDataColumnDir::getColumnDirId).collect(Collectors.toList());

递归——获取所有该父目录下的所有目录:

    /**
     * 递归——获取所有该父目录下的所有目录
     * @param dirId
     * @return
     */
    public List<ZmgrDataColumnDir> selectByParentDirId(List<ZmgrDataColumnDir> zmgrDataColumnDirList, Long dirId) {
        ZmgrDataColumnDir zmgrDataColumnDir = new ZmgrDataColumnDir();
        zmgrDataColumnDir.setColumnDirId(dirId);
        zmgrDataColumnDirList.add(zmgrDataColumnDir);

        ZmgrDataColumnDirExample zmgrDataColumnDirExample = new ZmgrDataColumnDirExample();
        zmgrDataColumnDirExample.createCriteria().andDirParentIdEqualTo(dirId);
        List<ZmgrDataColumnDir> dirParentIdList = zmgrDataColumnDirMapper.selectByExample(zmgrDataColumnDirExample);
        for(ZmgrDataColumnDir type : dirParentIdList) {
            // 递归
            selectByParentDirId(zmgrDataColumnDirList, type.getColumnDirId());
        }
        return zmgrDataColumnDirList;
    }

 

测试:

(1)表目录 说明:父目录id是3101,他包括他的的所有子目录是3101、3103、3104、2105四个

(2) 代码debug

 

good !

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值