数据库层级关系转换为树结构的json

原文链接:https://blog.csdn.net/Tonysdyp/article/details/80987959

首先是数据库设计:

通过查询数据库,将全局数据作为一个ArrayList<HashMap<String,Object>>获取

每单个节点都是一个HashMap,如

”cid”:2001,”cname”:华为,”pid”:1001

如下图

数据准备完成,接下来开始进行树结构转换

因为每一个节点现在都是以HashMap的格式来存储的,所以可以在HashMap里面添加一个key,用来存储子节点的信息,也就是baby,然后将拥有本节点cid为父节点pid的节点信息存入此节点的key为baby的ArrayList<HashMap<String,Object>>里面,同时加入递归,一层一层查下去,由此可以生成一个树结构的数据,然后使用json转换工具,就可以直接返回给浏览器

public List TreeGood(ArrayList<HashMap<String, Object>> notes, String cid) throws SQLException {
        ArrayList<HashMap<String, Object>> treeGoods = new ArrayList<HashMap<String, Object>>();
        for (HashMap<String, Object> good : notes) {
            System.out.println(cid);
            String pid = good.get("pid") == null ? "" : good.get("pid") + "";
            if (cid.equals(pid)) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                String ccid = (String) good.get("cid");
                map = good;
                map.put("baby", TreeGood(notes, ccid));
                treeGoods.add(map);
            }
        }
        return treeGoods;
}

扩展

默认查出所有分类字段,设置的根节点的父类id 为0

 //根据classfiyId,查询分类列表
    public List selectFindList(String itemId,String classfiyId){
//根据项目id,查询所有分类
        ArrayList<HashMap<String, Object>> list= classfiyMapper.selectList(itemId);
        List list1=new ArrayList();
        try {
            list1= TreeGood(list,classfiyId);
            System.out.println(list1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list1 ;
    }
/*递归储存为树状图结构*/
    public List TreeGood(ArrayList<HashMap<String, Object>> notes, String cid) throws SQLException {
        ArrayList<HashMap<String, Object>> treeGoods = new ArrayList<>();

        for (HashMap<String, Object> good : notes) {

            String pid = good.get("parentId") .equals("0") ? "0" : good.get("parentId") + "";
//cid 为规定父类id,可查询规定的分支为根目录
            if (cid.equals(pid)) {
                HashMap<String, Object> map;
                String ccid = (String) good.get("classfiyId");
                map = good;
                map.put("children", TreeGood(notes, ccid));
                treeGoods.add(map);
            }
        }
        return treeGoods;
    }

 结果:

[{
"showState": 1,
"children": [{
"showState": 0,
"children": [{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 4,
"classfiyId": "3b9254f8-ac0d-43e9-a205-18350d4a0f9b",
"title": "铸造生铁",
"parentId": "57f5289a-8dc7-4aa3-96be-b8776291603c"
},
{
"showState": 0,
"children": [],
"classfiyType": 4,
"classfiyId": "bd5adfcb-22e7-4c20-92c9-0f2b202d21b2",
"title": "钛合金",
"parentId": "57f5289a-8dc7-4aa3-96be-b8776291603c"
}
],
"classfiyType": 3,
"classfiyId": "57f5289a-8dc7-4aa3-96be-b8776291603c",
"title": "生铁",
"parentId": "cae3ee7b-fac5-48f2-bc4d-690dad581d51"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "c74eb1db-536d-4bad-a79d-3fc79fdc5e26",
"title": "方钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "459a9b33-573d-4ab4-821f-70d8aff58015",
"title": "扁钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "6e7d2aa6-9449-484c-939d-d1dc433646b2",
"title": "槽钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "5cf501ac-fba2-4a00-b3da-2f575916a331",
"title": "角钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "85ceea86-5382-4c1c-8eb0-d5da8d63a013",
"title": "工字钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "41eb40db-7d4d-40e8-a7aa-77c2dce820fd",
"title": "六角钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
}
],
"classfiyType": 4,
"classfiyId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd",
"title": "型钢",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "44d3808d-0043-45d2-8da0-ed5d63041736",
"title": "无缝管",
"parentId": "ff70bc9a-f2c5-4287-8ff4-68d85eb3bd1c"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "a0b9cbab-b09f-445a-bf2b-caeb3541c19e",
"title": "焊管",
"parentId": "ff70bc9a-f2c5-4287-8ff4-68d85eb3bd1c"
}
],
"classfiyType": 4,
"classfiyId": "ff70bc9a-f2c5-4287-8ff4-68d85eb3bd1c",
"title": "管材",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "dd608087-8176-466b-8a9c-b5976d064b1d",
"title": "薄钢板",
"parentId": "c8ba64ee-bf9a-44a6-9358-f674952e25a3"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "87359155-131c-4f53-a9c4-a1fa2df36c08",
"title": "中厚钢板",
"parentId": "c8ba64ee-bf9a-44a6-9358-f674952e25a3"
}
],
"classfiyType": 4,
"classfiyId": "c8ba64ee-bf9a-44a6-9358-f674952e25a3",
"title": "钢板",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "ea2ae294-0c55-4bc5-93c8-34667c40def9",
"title": "钢丝绳",
"parentId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "20571539-8aa1-4f9d-8b54-021320c80062",
"title": "钢绞线",
"parentId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "362999ed-564a-4245-8ce8-ce25a8a45124",
"title": "镀锌钢绞线",
"parentId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b"
}
],
"classfiyType": 4,
"classfiyId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b",
"title": "钢丝绳,绞线",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "f0439782-86bd-4529-8932-db31f017f99c",
"title": "圆钢",
"parentId": "f60dbd72-5537-4371-aed0-52b742785c9e"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "da855976-03eb-4b24-b3f9-6ee2c09fa52a",
"title": "螺纹钢",
"parentId": "f60dbd72-5537-4371-aed0-52b742785c9e"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "93d55bd6-dd41-4230-b4d5-cb5dc0f8684f",
"title": "线材",
"parentId": "f60dbd72-5537-4371-aed0-52b742785c9e"
}
],
"classfiyType": 4,
"classfiyId": "f60dbd72-5537-4371-aed0-52b742785c9e",
"title": "钢筋",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
}
],
"classfiyType": 3,
"classfiyId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3",
"title": "钢材",
"parentId": "cae3ee7b-fac5-48f2-bc4d-690dad581d51"
}
],
"classfiyType": 2,
"classfiyId": "cae3ee7b-fac5-48f2-bc4d-690dad581d51",
"title": "黑色金属",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 3,
"classfiyId": "7a67f4a1-c5b1-44ce-84ed-2c2d990bbc93",
"title": "铝及铝合金",
"parentId": "eff8cc03-8475-4f0b-8a55-b05c027b6794"
},
{
"showState": 0,
"children": [],
"classfiyType": 3,
"classfiyId": "4b0f83ed-0064-42ad-81a2-95c76793b37f",
"title": "铜及铜合金",
"parentId": "eff8cc03-8475-4f0b-8a55-b05c027b6794"
}
],
"classfiyType": 2,
"classfiyId": "eff8cc03-8475-4f0b-8a55-b05c027b6794",
"title": "有色金属",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "40de0d21-39f5-458c-a0fc-5b3afb244db3",
"title": "祖母绿",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "fb8a6f2a-e30c-4dc2-8772-86d259578ed8",
"title": "未命名",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [],
"classfiyType": 3,
"classfiyId": "57ca6782-b070-47a8-9917-86ed5c8f52ae",
"title": "未命名",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
}
],
"classfiyType": 1,
"classfiyId": "94011477-40fe-4870-ad09-dce762153319",
"title": "金属",
"parentId": "0"
},
{
"showState": 1,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "a0b6867d-2018-4add-89dd-9745d600acfd",
"title": "普通水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "0a5b35e9-f1fa-47eb-bc33-d803b450986c",
"title": "快硬高强水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "c7cba38d-58c0-49e4-b136-6424a0be5dd6",
"title": "耐侵蚀水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "2b160f60-d3f0-4963-a5a5-069a6565d180",
"title": "膨胀水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
}
],
"classfiyType": 1,
"classfiyId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b",
"title": "水泥",
"parentId": "0"
},
{
"showState": 1,
"children": [],
"classfiyType": 1,
"classfiyId": "f6cb44b3-b282-4730-b0e2-e3c313bbe52e",
"title": "木材",
"parentId": "0"
},
{
"showState": 0,
"children": [],
"classfiyType": 1,
"classfiyId": "a12e7bf2-b9ed-475c-8f5b-f14def130d46",
"title": "测试",
"parentId": "0"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "4b72ce4a-3923-4f77-abc3-6eed659af041",
"title": "未命名",
"parentId": "0"
}
]

猜你喜欢

转载自www.cnblogs.com/dk2557/p/12575063.html