1. 程式人生 > >關於有層級關係時返回前端樹狀json

關於有層級關係時返回前端樹狀json

結果
{
    "message": [
        {
            "children": [
                {
                    "children": [
                        {
                            "children": [
                                {
                                    "empno": "7876",
                                    
"ename": "ADAMS", "mgr": "7788", "sal": "1100" } ], "empno": "7788", "ename": "SCOTT",
"mgr": "7566", "sal": "3000" }, { "children": [ { "empno": "7369", "ename": "SMITH",
"mgr": "7902", "sal": "800" } ], "empno": "7902", "ename": "FORD", "mgr": "7566", "sal": "3000" } ], "empno": "7566", "ename": "JONES", "mgr": "7839", "sal": "2975" }, { "children": [ { "empno": "7499", "ename": "ALLEN", "mgr": "7698", "sal": "1600" }, { "empno": "7521", "ename": "WARD", "mgr": "7698", "sal": "1250" }, { "empno": "7654", "ename": "MARTIN", "mgr": "7698", "sal": "1250" }, { "empno": "7844", "ename": "TURNER", "mgr": "7698", "sal": "1500" }, { "empno": "7900", "ename": "JAMES", "mgr": "7698", "sal": "950" } ], "empno": "7698", "ename": "BLAKE", "mgr": "7839", "sal": "2850" }, { "children": [ { "empno": "7934", "ename": "MILLER", "mgr": "7782", "sal": "1300" } ], "empno": "7782", "ename": "CLARK", "mgr": "7839", "sal": "2450" } ], "empno": "7839", "ename": "KING", "mgr": "0", "sal": "5000" } ] }

 

1:資料庫為Oracle的emp表找出King的所有下屬

<select id="testBigDecimal25"  resultType="com.mayi.vo.Menu">
		select empno, ename, sal, mgr from emp
</select>

  

一個javaBean 

 

public class Menu {
     private String empno;
        private String ename;
        private String sal;
        private String mgr;
        
        //選單標識
       // private Meta meta;
     
        //子選單
        private List<Menu> children;


get set自補

 

遞迴查子

public JSONObject getInfo() {
	System.out.println("iiiii");
	List<Menu>	list=dao.testBigDecimal24(s1.concat(".testBigDecimal25"), null);
	System.out.println(list);
	
	//呼叫TreeTest工具類方法生成樹形結構的List集合
    List<Menu> treeList = TreeUtil.listToTree(list);

    //使用fastjson對樹形list件序列化轉成json字串,過濾掉屬性值為null的屬性
    String message = JSON.toJSONString(treeList,SerializerFeature.PrettyFormat);
    JSONObject returnData = new JSONObject();

    //重新將json字串轉成jsonObject物件,返回給前端
    returnData.put("message",JSON.parse(message));
    //return CommonUtil.successJson(returnData);

		return returnData;
		
	}

  utils

//用List構建帶有層次結構的json資料
//List父子節點構造樹形Json
public class TreeUtil {
  //將list集合轉成樹形結構的list集合
  public static List<Menu> listToTree(List<Menu> list) {
      //用遞迴找子。
      List<Menu> treeList = new ArrayList<Menu>();
      for (Menu tree : list) {
          if (tree.getMgr().equals("0")) {      //這個地方注意報空指標======
              treeList.add(findChildren(tree, list));
          }
      }
      return treeList;
  }

  //尋找子節點
  private static Menu findChildren(Menu tree, List<Menu> list) {
      for (Menu node : list) {
          if (node.getMgr().equals(tree.getEmpno())) {
              if (tree.getChildren() == null) {
                  tree.setChildren(new ArrayList<Menu>());
              }
              tree.getChildren().add(findChildren(node, list));
          }
      }
      return tree;
  }


}