根据顶层的id递归查询出全部子节点

效果图

根据输入的id为2查询出所有的红色框起来的节点

mapper接口

java 复制代码
TSystemOrg getOrgByorgId(String orgId);
List<TSystemOrg> getOrgListByParentId(String parentId);

mapper.xml

xml 复制代码
<!--根据id查询org-->
<select id="getOrgByorgId" resultType="com.iflytek.core.entity.TSystemOrg">
   SELECT
      *
   FROM
      t_system_org
   WHERE
      id=#{orgId}
   and
      is_delete = '0'
</select>

<!--根据parentId查询orgList-->
<select id="getOrgListByParentId" resultType="com.iflytek.core.entity.TSystemOrg">
   SELECT
      *
   FROM
      t_system_org
   WHERE
      parent_id=#{parentId}
   and
      is_delete = '0'
</select>

service接口

java 复制代码
List<TSystemOrg> getOrgListByOrgId(String orgId);

serviceImpl实现

java 复制代码
@Override
public List<TSystemOrg> getOrgListByOrgId(String orgId) {
    TSystemOrg org = mapper.getOrgByorgId(orgId);
    List<TSystemOrg> resultList = new ArrayList<>();
    resultList.add(org);
    diguiGetOrgListByParentId(org, resultList);
    return resultList;
}

private void diguiGetOrgListByParentId(TSystemOrg org, List<TSystemOrg> resultList) {
    List<TSystemOrg> orgList = mapper.getOrgListByParentId(org.getId());
    if(orgList != null){
        for(TSystemOrg o : orgList){
            resultList.add(o);
            //递归调用自己
            diguiGetOrgListByParentId(o,resultList);
        }
    }
}

controller

java 复制代码
@ApiOperation(value = "根据orgId获取所有子节点",tags = "机构")
@PostMapping("getOrgListByOrgId")
public ResultDto getOrgListByOrgId(String orgId){
    return ResultUtil.success("success", service.getOrgListByOrgId(orgId));
}
相关推荐
Mahir084 小时前
Spring 循环依赖深度解密:从问题本质到三级缓存源码级解析
java·后端·spring·缓存·面试·循环依赖·三级缓存
RyFit5 小时前
SpringAI 常见问题及解决方案大全
java·ai
石山代码5 小时前
C++ 内存分区 堆区
java·开发语言·c++
绝知此事6 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
无风听海6 小时前
C# 隐式转换深度解析
java·开发语言·c#
一只大袋鼠6 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
德思特7 小时前
从 Dify 配置页理解 RAG 的重要参数
java·人工智能·llm·dify·rag
YOU OU7 小时前
Spring IoC&DI
java·数据库·spring
один but you8 小时前
从可变参数到 emplace:现代 C++ 性能优化的核心组合
java·开发语言
是码龙不是码农8 小时前
ThreadPoolExecutor 7 个核心参数详解
java·线程池·threadpool