递归的组织树的格式

这段代码展示了如何通过递归方法获取并组织部门树结构。首先查询顶级部门,然后获取其下属人员列表,接着递归查询每个部门的子部门及其对应的人员,将结果存储在Map中。最终将构建好的组织树结构返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    public void getTree() {
        String parentId="0";
        Map<String, Object> map = new HashMap<>();
        try {
            Department province =  deptService.selectDeptMentByParentId(parentId).get(0);; //查询出一个部门
            List<Personnel>  personnelList= personnelServer.selectByDeptId(province.getDepartmentId().toString());    //查询部门下的人员
            province.put("personnelMap",personnelList);

            if (province != null) {
                List<Department> citys = deptService.selectDeptMentByParentId(province.getDepartmentId().toString());//查询省下面的所有市
                digui(citys); //调用递归算法查询市以下的区县
//                province.setChildren(citys);
                province.put("childer",citys);
            }
            map.put("data", province);
        } catch (Exception e) {
            e.printStackTrace();
        }

        renderJson(new Record().set("result", 1).set("data",map));
    }

    public void digui(List<Department> citys) {
        List<Department> retList = new ArrayList<>();
        for (Department c : citys) {
            retList = deptService.selectDeptMentByParentId(c.getDepartmentId().toString());
            List<Personnel>  personnelList= personnelServer.selectByDeptId(c.getDepartmentId().toString());    //查询部门下的人员
            c.put("personnelMap",personnelList);
            if (retList.size() > 0) {
                c.put("childer",retList);
                digui(retList); //循环调用自己
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值