根据顶层的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));
}
相关推荐
金銀銅鐵6 小时前
用 Swing 重做操作数栈演示
java·jvm·后端
琳少_6 小时前
java程序开发笔记
java·开发语言·数据库·笔记
叼烟扛炮8 小时前
[LangChain] 学习笔记(15):为什么你的重试没用——429、抖动、fallback 与熔断
java·服务器·前端
文丑颜不良啊9 小时前
LeetCode3401~3420题目解析
java·leetcode
文丑颜不良啊10 小时前
LeetCode3421~3440题目解析
java·leetcode
code2cat11 小时前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
码云社区12 小时前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
码云社区13 小时前
Spring Boot智慧物业系统源码:合同到期提醒、续签资料与资产协作
java·spring boot·后端·物业管理·物业系统·智慧物业
AI开源软件13 小时前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
陈如水13 小时前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring