日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java中使用递归算法实现子级架构的查询

發(fā)布時(shí)間:2025/3/19 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中使用递归算法实现子级架构的查询 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

場(chǎng)景

在實(shí)現(xiàn)企業(yè)架構(gòu)管理時(shí)采用樹形結(jié)構(gòu)。如圖:

現(xiàn)在要根據(jù)傳遞的id屬性查詢其有多少個(gè)子級(jí)架構(gòu)。

注:如果A的id是B的pid,那么A就是B的父級(jí)。

實(shí)現(xiàn)

遞歸函數(shù)如下:

public void selectChild(List<Long> ids){//用來存取調(diào)用自身遞歸時(shí)的參數(shù)List<Long> temp= new ArrayList<Long>();//查詢數(shù)據(jù)庫(kù)中對(duì)應(yīng)id的實(shí)體類List<SysEnterpriseOrg> sysEnterpriseOrgList = new ArrayList<SysEnterpriseOrg>();//遍歷傳遞過來的參數(shù)idsfor (Long id :ids) {//查詢子級(jí)架構(gòu)//此處使用mybaatisPlus的條件構(gòu)造器,查詢pid等于id的對(duì)象QueryWrapper<SysEnterpriseOrg> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<SysEnterpriseOrg>();sysEnterpriseOrgChildQueryWrapper.eq("pid",id.toString());//查詢結(jié)果返會(huì)一個(gè)listsysEnterpriseOrgList= sysEnterpriseOrgMapper.selectList(sysEnterpriseOrgChildQueryWrapper);//遍歷list獲取符合條件的對(duì)象的id值,一份存到temp中用作遞歸的參數(shù),并存到全局變量中用來獲取所有符合條件的idfor (SysEnterpriseOrg s:sysEnterpriseOrgList) {temp.add(s.getId());result.add(s.getId());}}if(temp.size()!=0&&temp!=null){selectChild(temp);}}

?

單元測(cè)試調(diào)用示例:

package com.ws.test.common;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ws.sys.entity.SysEnterpriseOrg; import com.ws.sys.mapper.SysEnterpriseOrgMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration;import java.util.ArrayList; import java.util.List;/*** Created by HAOHAO on 2019/6/18.*/ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest @WebAppConfiguration public class diguiTest? {@Autowiredprivate SysEnterpriseOrgMapper sysEnterpriseOrgMapper;List<Long> result = new ArrayList<Long>();@Testpublic void test(){List<Long> canshu = new ArrayList<Long>();canshu.add(1l);selectChild(canshu);for (Long s :result) {System.out.print(s);}}public void selectChild(List<Long> ids){//用來存取調(diào)用自身遞歸時(shí)的參數(shù)List<Long> temp= new ArrayList<Long>();//查詢數(shù)據(jù)庫(kù)中對(duì)應(yīng)id的實(shí)體類List<SysEnterpriseOrg> sysEnterpriseOrgList = new ArrayList<SysEnterpriseOrg>();//遍歷傳遞過來的參數(shù)idsfor (Long id :ids) {//查詢子級(jí)架構(gòu)//此處使用mybaatisPlus的條件構(gòu)造器,查詢pid等于id的對(duì)象QueryWrapper<SysEnterpriseOrg> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<SysEnterpriseOrg>();sysEnterpriseOrgChildQueryWrapper.eq("pid",id.toString());//查詢結(jié)果返會(huì)一個(gè)listsysEnterpriseOrgList= sysEnterpriseOrgMapper.selectList(sysEnterpriseOrgChildQueryWrapper);//遍歷list獲取符合條件的對(duì)象的id值,一份存到temp中用作遞歸的參數(shù),并存到全局變量中用來獲取所有符合條件的idfor (SysEnterpriseOrg s:sysEnterpriseOrgList) {temp.add(s.getId());result.add(s.getId());}}if(temp.size()!=0&&temp!=null){selectChild(temp);}} }

?


?

總結(jié)

以上是生活随笔為你收集整理的Java中使用递归算法实现子级架构的查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。