日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

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

場(chǎng)景

在實(shí)現(xiàn)企業(yè)架構(gòu)管理時(shí)采用樹(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){//用來(lái)存取調(diào)用自身遞歸時(shí)的參數(shù)List<Long> temp= new ArrayList<Long>();//查詢數(shù)據(jù)庫(kù)中對(duì)應(yīng)id的實(shí)體類List<SysEnterpriseOrg> sysEnterpriseOrgList = new ArrayList<SysEnterpriseOrg>();//遍歷傳遞過(guò)來(lái)的參數(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ù),并存到全局變量中用來(lái)獲取所有符合條件的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){//用來(lái)存取調(diào)用自身遞歸時(shí)的參數(shù)List<Long> temp= new ArrayList<Long>();//查詢數(shù)據(jù)庫(kù)中對(duì)應(yīng)id的實(shí)體類List<SysEnterpriseOrg> sysEnterpriseOrgList = new ArrayList<SysEnterpriseOrg>();//遍歷傳遞過(guò)來(lái)的參數(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ù),并存到全局變量中用來(lái)獲取所有符合條件的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èn)題。

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