Lien original: https: //blog.csdn.net/Tonysdyp/article/details/80987959
Le premier est la conception de base de données:
En interrogeant la base de données, les données globales comme ArrayList <HashMap <String, Object >> acquisition
Chaque noeud particulier est un HashMap, tel que
"Cid": 2001, "CNAME": Huawei, "pid": 1001
Comme indiqué ci-dessous
Données prêt, puis commencer à l'arbre de conversion
Parce que chaque noeud sont maintenant stockés dans le format HashMap, peut être ajouté dans une clé HashMap qui est utilisé pour stocker les informations des nœuds enfants, à savoir bébé, puis le nœud actuel a un nœud parent de stockage d'informations de noeud cid de pid dans cette clé de noeud est le bébé ArrayList <HashMap <String, Object >> à l'intérieur, tout en ajoutant de récursivité, la recherche se poursuit couche par couche, en générant ainsi une structure de données d'arbre, puis l'outil de conversion json, il peut être directement renvoyé au navigateur
publique Liste TreeGood (ArrayList <HashMap <String, Object >> notes, String cid) jette SQLException { ArrayList <HashMap <String, Object >> treeGoods = nouveau ArrayList <HashMap <String, Object >> (); pour (HashMap <String, Object> bon: notes) { System.out.println (cid); Chaîne pid = good.get ( "pid") == null ? "": Good.get ( "pid") + "" ; si (cid.equals (pid)) { HashMap <String, Object> = carte nouvelle HashMap <String, Object> ();= (String) good.get ( "cid" ); carte = bonne; map.put ( "bébé" , TreeGood (notes, CCID)); treeGoods.add (carte); } } Retourner treeGoods; }
propagation
Par défaut savoir tout le champ classé, définissez le nœud racine de l'ID de classe parente 0
// Selon classfiyId, liste de classification de requête publique Liste selectFindList (itemId String, String classfiyId) { // Selon l'ID du projet, Toutes les catégories ArrayList <HashMap <String, Object >> List = classfiyMapper.selectList (itemId) Liste Liste1 = new new ArrayList (); try { Liste1 = TreeGood (Liste, classfiyId); System.out.println (Liste1); } les prises (SQLException E) { e.printStackTrace (); } retour Liste1; }
/ * Récursive stockée en tant que structure de l'arborescence * / publique Liste TreeGood (ArrayList <HashMap <String, Object >> Notes, chaîne CID) jette SQLException { ArrayList <HashMap <String, Object >> treeGoods = new new ArrayList <> (); pour (le HashMap <String, Object> Bon: notes) { chaîne PID = good.get ( "le parentId") equals ( "0") "0" good.get :? ( "le parentId") + "" ; // cid id classe parente prédéterminée, une requête peut être spécifiée comme une racine de branche SI (cid.equals (PID)) { HashMap <String, Object> Carte; Chaîne CCID = (String) good.get ( "classfiyId" ); carte = bonne; map.put ( "enfants" , TreeGood (notes, CCID)); treeGoods.add (carte); } } Retourner treeGoods; }
résultats:
[{
"ShowState": 1,
"enfants": [{
"showState": 0,
"enfants": [{
"showState": 0,
"enfants": [{
"showState": 0,
"enfants": [] ,
"classfiyType": 4,
"classfiyId": "3b9254f8-ac0d-43e9-a205-18350d4a0f9b",
"title": "铸造生铁",
"parentId": "57f5289a-8dc7-4aa3-96be-b8776291603c"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [{
"showState": 0,
"enfants": [{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "c74eb1db-536d-4bad-a79d-3fc79fdc5e26",
"title": "方钢",
"parentId": « ba595ced -cc2d-42b6-b004-78e9a4ada2fd »
},
{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "459a9b33-573d-4ab4-821f-70d8aff58015",
"title": "扁钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "6e7d2aa6-9449-484c-939d-d1dc433646b2",
"title": "槽钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
" showState ": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "5cf501ac-fba2-4a00-b3da-2f575916a331",
"title": "角钢",
"parentId":" ba595ced-cc2d -42b6-b004-78e9a4ada2fd "
},
{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "85ceea86-5382-4c1c-8eb0-d5da8d63a013",
"title ": "工字钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "44d3808d-0043-45d2-8da0-ed5d63041736",
"title ": "无缝管",
"parentId": "ff70bc9a-f2c5-4287-8ff4-68d85eb3bd1c"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "dd608087-8176-466b-8a9c-b5976d064b1d",
"title ": "薄钢板",
"parentId": "c8ba64ee-bf9a-44a6-9358-f674952e25a3"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "ea2ae294-0c55-4bc5-93c8-34667c40def9",
"title ": "钢丝绳",
"parentId": "206b9822-BF2E-4684-bec1-7fe5d5eab45b"
},
{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "20571539-8aa1-4f9d-8b54-021320c80062",
"title": "钢绞线",
"parentId": "206b9822-BF2E-4684-bec1-7fe5d5eab45b"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [{
"showState": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "f0439782-86bd-4529-8932-db31f017f99c",
"title": "圆钢",
"parentId": "f60dbd72-5537-4371-aed0-52b742785c9e"
},
{
" showState ": 0,
"enfants": [],
"classfiyType": 5,
"classfiyId": "da855976-03eb-4b24-b3f9-6ee2c09fa52a",
"title": "螺纹钢",
"parentId":" f60dbd72- 5537-4371-aed0-52b742785c9e "
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [{
"showState": 0,
"enfants": [],
"classfiyType": 3,
"classfiyId": "7a67f4a1-c5b1-44ce-84ed-2c2d990bbc93",
"title": "铝及铝合金",
"parentId": "eff8cc03-8475-4f0b-8a55-b05c027b6794"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants ": [],
"classfiyType": 2,
"classfiyId": "40de0d21-39f5-458c-a0fc-5b3afb244db3",
"title": "祖母绿",
"parentId": "94011477-40fe-4870-AD09-dce762153319"
},
{
"showState": 0,
"enfants": [],
"classfiyType": 2,
"classfiyId": « fb8a6f2a-e30c-4dc2-8772- 86d259578ed8" ,
"title": "未命名",
"parentId": "94011477-40fe-4870-AD09-dce762153319"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [{
"showState": 0,
"enfants": [],
"classfiyType": 2,
"classfiyId": "a0b6867d-2018-4add-89dd-9745d600acfd",
"title" : "普通水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"enfants": [],
"classfiyType": 2,
"classfiyId": « 0a5b35e9- f1fa-47eb-BC33-d803b450986c "
"title": "快硬高强水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"enfants ": [],
"classfiyType": 2,
"classfiyId": "c7cba38d-58c0-49e4-b136-6424a0be5dd6",
"title": "耐侵蚀水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"enfants": [],
"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,
"enfants": [],
"classfiyType": 1,
"classfiyId ": "f6cb44b3-b282-4730-b0e2-e3c313bbe52e",
"title": "木材",
"parentId": "0"
},
{
"showState": 0,
"enfants": [],
"classfiyType": 1,
"classfiyId": "a12e7bf2-b9ed-475C-8f5b-f14def130d46",
"title": "测试",
"parentId": "0"
},
{
"showState" : 0,
"enfants": [],
"classfiyType": 2,
"classfiyId": "4b72ce4a-3923-4f77-abc3-6eed659af041",
"title": "未命名",
"parentId": "0"
}
]