聯(lián)合MyBatis,由Map獲取
專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)九原免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
源碼如下
package com.zl.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Java遞歸獲取TreeJson
* @author ZhangLi
* @date 2019年6月18日 上午11:10:45
* @WeiXin zl4828
* @備注 沫沫金原創(chuàng)提供,僅供參考
*/
public class TreeJson {
public static void main(String[] args) {
TreeJson treeJson = new TreeJson();
treeJson.treeMap.add(new HashMap(){{
put("ID","0");
put("NAME","根節(jié)點");
put("PID","null");
}});
treeJson.treeMap.add(new HashMap(){{
put("ID","1");
put("NAME","節(jié)點1");
put("PID","0");
}});
treeJson.treeMap.add(new HashMap(){{
put("ID","2");
put("NAME","節(jié)點2");
put("PID","0");
}});
treeJson.treeMap.add(new HashMap(){{
put("ID","3");
put("NAME","節(jié)點3");
put("PID","0");
}});
treeJson.treeMap.add(new HashMap(){{
put("ID","11");
put("NAME","節(jié)點11");
put("PID","1");
}});
treeJson.treeMap.add(new HashMap(){{
put("ID","12");
put("NAME","節(jié)點12");
put("PID","1");
}});
treeJson.treeMap.add(new HashMap(){{
put("ID","13");
put("NAME","節(jié)點13");
put("PID","1");
}});
System.out.println(treeJson.recursiveTree("0"));
}
private List
應(yīng)用場景
例如,Echarts樹形圖形
注意,使用JSON轉(zhuǎn)換
{
name: '根節(jié)點',
pid: null,
id: 0,
children: [{
name: '節(jié)點1',
pid: 0,
id: 1,
children: [{
name: '節(jié)點11',
pid: 1,
id: 11,
children: []
}, {
name: '節(jié)點12',
pid: 1,
id: 12,
children: []
}, {
name: '節(jié)點13',
pid: 1,
id: 13,
children: []
}]
}, {
name: '節(jié)點2',
pid: 0,
id: 2,
children: []
}, {
name: '節(jié)點3',
pid: 0,
id: 3,
children: []
}]
};
以上,拷貝本地運行,任意應(yīng)用。