树查询数据库后以树的形式封装起来

文章描述了一个Java程序,通过数据库查询获取部门信息和人员集合,然后使用递归方法将这些信息组织成树形结构,以便前端展示。UserDeptDto类用于存储部门数据,包括部门ID、名称、父级ID和子部门/人员列表。
摘要由CSDN通过智能技术生成

记个笔记,方便以后查阅

逻辑代码:

        //定义最终返回值
        List<UserDeptDto> respList = new ArrayList<>();

        //查询 每个部门信息 以及 旗下的人员集合
        List<UserDeptDto> allUserWithOutAdmin = userMapper.getAllUserWithOutAdmin();
        if (null == allUserWithOutAdmin || allUserWithOutAdmin.isEmpty()) {
            return respList;
        }

        Map<Long, UserDeptDto> tempMap = allUserWithOutAdmin.stream()
                .collect(Collectors.toMap(UserDeptDto::getDeptId, v -> v, (oldKey, newKey) -> newKey));
        
        allUserWithOutAdmin.stream().forEach(item->{

            if (item.getParentId() == 0) {
                respList.add(item);//如果父节点为0,表命一级部门,直接录入
            }

            //获取父节点
            UserDeptDto userDeptDto = tempMap.get(item.getParentId());
            if (null != userDeptDto) {
                if (null == userDeptDto.getChildDeptList()) {
                    userDeptDto.setChildDeptList(new ArrayList<>());
                }
                userDeptDto.getChildDeptList().add(item);
            }
        });

        return respList;

封装到的实体类 :

public class UserDeptDto {


    @Schema(description = "部门编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long deptId;

    @Schema(description = "部门名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "研发部")
    private String deptName;

    @Schema(description = "部门父级id", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Long parentId;

    @Schema(description = "该部门的子部门集合")
    private List<UserDeptDto> childDeptList;

    @Schema(description = "每个部门下的人员信息集合")
    private List<User> userVoList;
}

2024-3-23 补充封装后的递归方法。前端要求人员List和 部门List 都放到childList里面,不要分开,所以在原来方法上进行一个递归封装。

//上方代码下插入
respList.forEach(item -> {
            UserDeptTreeRespVo respVo = new UserDeptTreeRespVo();
            foramtData(respVo, item);
            treeRespVoList.add(respVo);
        });




//formatData递归封装方法
/**
     * 递归重新封装数据
     *
     * @param dto
     */
    private void foramtData(UserDeptTreeRespVo respVo, UserDeptDto dto) {
        respVo.setName(dto.getDeptName());
        respVo.setParentId("###" + dto.getParentId());
        respVo.setId("###" + dto.getDeptId());
        if (null == respVo.getChildList()) {
            respVo.setChildList(new ArrayList<>());
        }
        //把用户集合塞进child
        if (null != dto.getUserVoList()) {
            dto.getUserVoList().forEach(item->{
                UserDeptTreeRespVo userRespVo = new UserDeptTreeRespVo();
                userRespVo.setId(item.getId()+"");
                userRespVo.setName(item.getUsername());
                respVo.getChildList().add(userRespVo);
            });
        }
        //把部门集合塞进child
        List<UserDeptDto> childDeptList = dto.getChildDeptList();
        if (null != childDeptList) {
            for (UserDeptDto userDeptDto : childDeptList) {
                UserDeptTreeRespVo userRespVo = new UserDeptTreeRespVo();

                if (null != userDeptDto.getChildDeptList()) {
                    //若部门后还有部门
                    foramtData(userRespVo, userDeptDto);
                } else {
                    //把该部门塞入child
                    userRespVo.setName(userDeptDto.getDeptName());
                    userRespVo.setParentId("###" + userDeptDto.getParentId());
                    userRespVo.setId("###" + userDeptDto.getDeptId());
                    userRespVo.setChildList(new ArrayList<>());
                    //把该部门人员塞入child
                    if (null != userDeptDto.getUserVoList()) {
                        userDeptDto.getUserVoList().forEach(item->{
                            UserDeptTreeRespVo temp = new UserDeptTreeRespVo();
                            temp.setId(item.getId()+"");
                            temp.setName(item.getUsername());
                            userRespVo.getChildList().add(temp);
                        });
                    }
                }
                respVo.getChildList().add(userRespVo);
            }
        }
    }

原来返回Json

    @Schema(description = "部门编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long deptId;

    @Schema(description = "部门名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "研发部")
    private String deptName;

    @Schema(description = "部门父级id", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Long parentId;

    @Schema(description = "该部门的子部门集合")
    private List<UserDeptDto> childDeptList;

    @Schema(description = "每个部门下的人员信息集合")
    private List<User> userVoList; 

{
            "deptId": "###100",
            "deptName": "河北小度",
            "parentId": 0,
            "childDeptList": [
                {
                    "deptId": "###101",
                    "deptName": "保定总公司",
                    "parentId": 100,
                    "childDeptList": [
                        {
                            "deptId": "###106",
                            "deptName": "财务部门",
                            "parentId": 101,
                            "childDeptList": null,
                            "userVoList": [
                                {
                                    "id": 103,
                                    "username": "源码"
                                }
                            ]
                        },
                        {
                            "deptId": "###107",
                            "deptName": "运维部门",
                            "parentId": 101,
                            "childDeptList": null,
                            "userVoList": [
                                {
                                    "id": 104,
                                    "username": "测试号"
                                }
                            ]
                        },
                        {
                            "deptId": "###104",
                            "deptName": "市场部门",
                            "parentId": 101,
                            "childDeptList": null,
                            "userVoList": [
                                {
                                    "id": 20,
                                    "username": "sc"
                                }
                            ]
                        },
                        {
                            "deptId": "###103",
                            "deptName": "研发部门",
                            "parentId": 101,
                            "childDeptList": null,
                            "userVoList": []
                        },
                        {
                            "deptId": "###105",
                            "deptName": "测试部门",
                            "parentId": 101,
                            "childDeptList": null,
                            "userVoList": []
                        }
                    ],
                    "userVoList": []
                },
                {
                    "deptId": "###102",
                    "deptName": "石家庄分公司",
                    "parentId": 100,
                    "childDeptList": [
                        {
                            "deptId": "###109",
                            "deptName": "财务部门",
                            "parentId": 102,
                            "childDeptList": null,
                            "userVoList": []
                        },
                        {
                            "deptId": "###108",
                            "deptName": "市场部门",
                            "parentId": 102,
                            "childDeptList": null,
                            "userVoList": []
                        }
                    ],
                    "userVoList": []
                }
            ],
            "userVoList": [
                {
                    "id": 115,
                    "username": "1"
                },
                {
                    "id": 23,
                    "username": "测试"
                },
                {
                    "id": 1,
                    "username": "河北小度"
                }
            ]
        },

现在返回Json数据

    @Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private String id;

    @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private String name;

    @Schema(description = "父级id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private String parentId;

    @Schema(description = "子集合", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private List<UserDeptTreeRespVo> childList; 


{
            "id": "###100",
            "name": "河北小度",
            "parentId": "###0",
            "childList": [
                {
                    "id": "115",
                    "name": "1",
                    "parentId": null,
                    "childList": null
                },
                {
                    "id": "23",
                    "name": "测试",
                    "parentId": null,
                    "childList": null
                },
                {
                    "id": "1",
                    "name": "河北小度",
                    "parentId": null,
                    "childList": null
                },
                {
                    "id": "###101",
                    "name": "保定总公司",
                    "parentId": "###100",
                    "childList": [
                        {
                            "id": "###106",
                            "name": "财务部门",
                            "parentId": "###101",
                            "childList": [
                                {
                                    "id": "103",
                                    "name": "源码",
                                    "parentId": null,
                                    "childList": null
                                }
                            ]
                        },
                        {
                            "id": "###107",
                            "name": "运维部门",
                            "parentId": "###101",
                            "childList": [
                                {
                                    "id": "104",
                                    "name": "测试号",
                                    "parentId": null,
                                    "childList": null
                                }
                            ]
                        },
                        {
                            "id": "###104",
                            "name": "市场部门",
                            "parentId": "###101",
                            "childList": [
                                {
                                    "id": "20",
                                    "name": "sc",
                                    "parentId": null,
                                    "childList": null
                                }
                            ]
                        },
                        {
                            "id": "###103",
                            "name": "研发部门",
                            "parentId": "###101",
                            "childList": []
                        },
                        {
                            "id": "###105",
                            "name": "测试部门",
                            "parentId": "###101",
                            "childList": []
                        }
                    ]
                },
                {
                    "id": "###102",
                    "name": "石家庄分公司",
                    "parentId": "###100",
                    "childList": [
                        {
                            "id": "###109",
                            "name": "财务部门",
                            "parentId": "###102",
                            "childList": []
                        },
                        {
                            "id": "###108",
                            "name": "市场部门",
                            "parentId": "###102",
                            "childList": []
                        }
                    ]
                }
            ]
        },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值