数据库树状结构数据查询

思路:

  1.根据条件先查询所有一级目录,将数据使用对象Object封装并将对象存放到集合list1中;

  2.遍历list1 ;

  3.根据条件查询一级目录各对应的子目录将数据存储到对象Object中;

  4.返回list1;

eg:

  SQL:

INSERT INTO gjyf_kpis.qaa_publish_portfolio_config (id,category_code,parent_code,publish_id,publish_allocation_id,category_name) VALUES
('1186921770763141120','combName','0','1186907623314702337','1186907623314702336','组合')
,('1186921770796695552','equity-reits','0','1186907623314702337','1186907623314702336','权益类')
,('1186921770830249984','stocks','equity-reits','1186907623314702337','1186907623314702336','股票')
,('1186921770863804416','hStock','equity-reits','1186907623314702337','1186907623314702336','港股')
,('1186921770901553152','covtBonds','equity-reits','1186907623314702337','1186907623314702336','转债')
,('1186921770930913280','fixed-income','0','1186907623314702337','1186907623314702336','固收类')
,('1186921770960273408','bonds','fixed-income','1186907623314702337','1186907623314702336','债券')
,('1186921770993827840','cash-assets','0','1186907623314702337','1186907623314702336','现金类')
,('1186921771035770880','monetarys','cash-assets','1186907623314702337','1186907623314702336','货币')
,('1186921771069325312','others-income','0','1186907623314702337','1186907623314702336','其它类')
;
INSERT INTO gjyf_kpis.qaa_publish_portfolio_config (id,category_code,parent_code,publish_id,publish_allocation_id,category_name) VALUES
('1186921771098685440','qdllStock','others-income','1186907623314702337','1186907623314702336','QDII股')
,('1186921771132239872','qdllBond','others-income','1186907623314702337','1186907623314702336','QDII债')
,('1186921771161600000','crude','others-income','1186907623314702337','1186907623314702336','原油')
,('1186921771199348736','hedgeFund','others-income','1186907623314702337','1186907623314702336','对冲基金')
;

  JAVA:

//查询数据结果封装类

import lombok.Data;

@Data
public class QaaKnv {
    
    private String name;
    
    private String value;
    
    private Object data;

    public QaaKnv(String name, String value) {
        super();
        this.name = name;
        this.value = value;
    }
    
    public QaaKnv(String key, String name, String value) {
        super();
        this.name = name;
        this.value = value;
    }

    public QaaKnv setData(Object data) {
        this.data = data;
        return this;
    }
    
}

 

 /**
     * 查询表头信息
     */
    public Object selectPublishPortfolioConfigHeader(String publishId, String publishAllocationId) {
     
        List publishPortfolioConfigs = this.selectPublishPortfolioConfig(publishId,publishAllocationId);
        
        List allResult = ListUtils.newArrayList();
        //一级
        for (PublishPortfolioConfig ppfc : publishPortfolioConfigs) {
            if("0".equals(ppfc.getParentCode())) {
                allResult.add(new QaaKnv(ppfc.getCategoryName(), ppfc.getCategoryCode()));
            }
        }
        //二级
        for (QaaKnv qaaKnv : allResult) {
            List leve2Kv= ListUtils.newArrayList();
            for (PublishPortfolioConfig ppfc : publishPortfolioConfigs) {
                if(!"0".equals(ppfc.getParentCode()) &&
                        ppfc.getParentCode().equals(qaaKnv.getValue())) {
                    
                    leve2Kv.add(new QaaKnv(ppfc.getCategoryName(), ppfc.getCategoryCode()));
                }
            }
            if(leve2Kv.size()>0) {
                qaaKnv.setData(leve2Kv);
            }
        }
        
        return allResult;
    }

  //测试结果

[
{
"name": "组合",
"value": "combName"
},
{
"name": "权益类",
"value": "equity-reits",
"data": [
{
"name": "股票",
"value": "stocks"
},
{
"name": "港股",
"value": "hStock"
},
{
"name": "转债",
"value": "covtBonds"
}
]
},
{
"name": "固收类",
"value": "fixed-income",
"data": [
{
"name": "债券",
"value": "bonds"
}
]
},
{
"name": "现金类",
"value": "cash-assets",
"data": [
{
"name": "货币",
"value": "monetarys"
}
]
},
{
"name": "其它类",
"value": "others-income",
"data": [
{
"name": "QDII股",
"value": "qdllStock"
},
{
"name": "QDII债",
"value": "qdllBond"
},
{
"name": "原油",
"value": "crude"
},
{
"name": "对冲基金",
"value": "hedgeFund"
}
]
}
]

你可能感兴趣的:(数据库树状结构数据查询)