1. Scénarios d'utilisation
Arbre de structure organisationnelle, il existe généralement un tableau de structure organisationnelle, y compris des champs tels que le code (code), le pCODE (code supérieur), le nom (nom de l'organisation)
2. Construire des données (les données suivantes ne sont pas des données organisationnelles, mais les données purement créées)
Liste <Tree <Stipter>> arbres = new ArrayList <Tree <Sest >> (); tests.add (nouveau test ("0", "", "About Me")); tests.add (nouveau test ("1", "0", "Intérêt technique")); Tests.Add (new test ("2", "0", "intérêt"); "JAVA"));tests.add(new Test("4", "1", "oracle"));tests.add(new Test("5", "1", "spring"));tests.add(new Test("6", "1", "springmvc"));tests.add(new Test("7", "1", "fastdfs"));tests.add(new Test("8", "1", "linux")); tests.add (nouveau test ("9", "2", "cyclisme")); tests.add (nouveau test ("10", "2", "manger, boire, play")); tests.add (nouveau test ("11", "2", "apprentissage")); tests.add (new test ("12", "" "," "string")); Tests.add (new test ("13", "", "", "string")); "4", "SQL")); tests.add (nouveau test ("14", "5", "IOC")); tests.add (nouveau test ("15", "5", "AOP"); Tests.Add (new Test ("17", ",", et "et"); ",", "4", "etc.); "etc.)); tests.add (nouveau test (" 19 "," 4 "," etc.)); tests.add (nouveau test ("20", "5", "etc.)); tests.add (nouveau test (" 20 "," 5 "," etc.));3. Code source
Arbre.java
Package pers.kangxu.datautils.bean.tree; import java.util.arraylist; import java.util.list; import java.util.map; import com.alibaba.fastjson.json; / ** * arbre todo <br> * * @author kangxu2 2017-1-7 * * / Classe publique Tree <T> @author kangxu2 2017-1-7 * * / Classe publique Tree <T> @Author Kangxu2 2017-1-7 * * / Public Class Tree / ** * Afficher le texte du nœud * / Texte de chaîne privée; / ** * Statut de nœud, ouvert fermé * / private String State = "Open"; / ** * Si le nœud est sélectionné True False * / privé boolean checked = false; / ** * Attribut nœud * / Liste privée <map <string, objet >> Attributs; / ** * Enfants du nœud * / Liste privée <Tree <T>> Enfants = New ArrayList <Tree <T>> (); / ** * Parent ID * / private String Parentid; / ** * s'il y a un nœud parent * / booléen privé isparent = false; / ** * s'il y a un nœud enfant * / ischildren booléen privé = false; public String getID () {return id; } public void setid (String id) {this.id = id; } public String getText () {return text; } public void setText (String Text) {this.text = text; } public String getState () {return State; } public void setState (String State) {this.state = state; } public boolean ischecked () {return vérifié; } public void setChecked (boolean checked) {this.checked = checked; } public list <map <string, objet >> getAtTributes () {return attributes; } public void setAttributes (list <map <string, object >> attributs) {this.attributes = attributs; } Liste publique <Tree <T>> getChildren () {return enfants; } public void setchildren (list <arbre <t>> enfants) {this.children = enfants; } public boolean isparent () {return isparent; } public void setparent (boolean isparent) {this.isparent = isparent; } public booléen ischildren () {return ischildren; } public void setchildren (booléen ischildren) {this.ischildren = ischildren; } public String getParentId () {return parentid; } public void setParentId (String parentid) {this.parentid = parentid; } arborescence publique (ID de chaîne, texte de chaîne, état de chaîne, booléen vérifié, liste <map <string, objet >> Attributs, list <Tree <T>> Enfants, booléen isparent, ischildren booléen, chaîne parentid) {super (); this.id = id; this.text = text; this.state = state; this.checked = vérifié; this.attributes = attributs; this.children = enfants; this.isparent = isparent; ce.schildren = ischildren; this.parentid = parentid; } public arbre () {super (); } @Override public String toString () {return json.tojSontring (this); }}Buildtree.java
Package pers.kangxu.datautils.common.tree; import java.util.arraylist; import java.util.list; import pers.kangxu.datautils.bean.tree.tree; / ** * build arbre * todo * <br> * @author kangxu2 2017-1-7 * * / Classe publique BuildTree {/ ** * TODO *; @Author Kangxu2 2017-1-7 * * @param nœuds * @return * / public static <T> Tree <T> build (list <Tree <T>> nœuds) {if (nœuds == null) {return null; } List <arbre <T>> topNodes = new ArrayList <Tree <T>> (); pour (arbre <t> enfants: nœuds) {string pid = enfants.getParentId (); if (pid == null || "" .equals (pid)) {topnodes.add (enfants); continuer; } pour (arbre <t> parent: nœuds) {string id = parent.getId (); if (id! = null && id.equals (pid)) {parent.getchildren (). add (enfants); enfants.Setparent (vrai); parent.Setchildren (true); continuer; }}} Arbre <t> root = new arbre <t> (); if (topnodes.size () == 0) {root = topnodes.get (0); } else {root.setid ("- 1"); root.setParentId (""); root.setparent (false); root.Setchildren (true); root.Setched (true); root.Setchildren (topNodes); root.setText ("nœud de niveau supérieur"); } return root; }}BuildTreeter.java
package pers.kangxu.datautils.test; import java.util.arraylist; import java.util.list; import pers.kangxu.datautils.bean.tree.tree; import Liste <arbre <test>> arbres = new ArrayList <Tree <Sest >> (); List <test> tests = new ArrayList <Sest> (); tests.add (nouveau test ("0", "", "About Me")); tests.add (nouveau test ("1", "0", "apprentissage technique")); Tests.Add (nouveau test ("2", "0", "intérêt")); tests.add (nouveau test ("3", "1", "java")); test.add (nouveau test ("4", "1", "oracle")); tests.add (nouveau test ("5", "1", "printemps")); tests.add (nouveau test ("6", "1", "springmvc")); Tests.Add (nouveau test ("7", "1", "FastDFS")); Tests.Add (nouveau test ("8", "1", "Linux")); Tests.Add (nouveau test ("9", "2", "cyclisme")); tests.add (nouveau test ("10", "2", "manger, boire et s'amuser")); tests.add (nouveau test ("11", "2", "apprentissage")); Tests.Add (nouveau test ("12", "3", "String")); Tests.Add (nouveau test ("13", "4", "SQL")); Tests.Add (nouveau test ("14", "5", "IOC")); Tests.Add (nouveau test ("15", "5", "AOP")); tests.add (nouveau test ("16", "1", "etc.)); tests.add (nouveau test ("17", "2", "etc.)); tests.add (nouveau test ("18", "3", "etc.)); tests.add (nouveau test ("19", "4", "etc.)); tests.add (nouveau test ("20", "5", "etc.)); pour (test de test: tests) {arbre <test> arbre = new Tree <Sest> (); Tree.SetId (test.getId ()); Tree.SetParentId (test.getPid ()); Tree.SeTText (test.getText ()); arbres.add (arbre); } Arbre <Sest> t = buildtree.build (arbres); System.out.println (t); }} Class Test {ID de chaîne privée; String privé pid; texte de chaîne privé; public String getID () {return id; } public void setid (String id) {this.id = id; } public String getpid () {return pid; } public void setPid (String pid) {this.pid = pid; } public String getText () {return text; } public void setText (String Text) {this.text = text; } test public (String id, String pid, String text) {super (); this.id = id; this.pid = pid; this.text = text; } public test () {super (); } @Override public String toString () {return "test [id =" + id + ", pid =" + pid + ", text =" + text + "]"; }}4. Résultats de l'opération
Données JSON:
{"vérifié": true, "enfants": [{"vérifié": false, "enfants": [{"vérifié": false, "enfants": [{"vérifié": false, "enfants": [{"vérifié": false, "enfants": [{"vérifié": false " "Text": "String"}, {"vérifié": false, "enfants": [], "id": "18", "parent": true, "parentid": "3", "state": "open", "text": "etc"}], "id": "3", "parent": true, "parentid": "1", ":": "open", "" false, "enfants": [{"vérifié": false, "enfants": [], "id": "13", "parent": true, "parentid": "4", "state": "open", "text": "sql"}, {"vérifié": false " "et }, { "checked": false, "children": [], "id": "15", "parent": true, "parentId": "5", "state": "open", "text": "aop" }, { "checked": false, "children": [], "id": "20", "parent": true, "parentId": "5", "state": "open", "text": "etc" } ], "id": "5", "parent": true, "parentid": "1", "state": "open", "text": "printemps"}, {"checked": false, "enfants": [], "id": "6", "parent": true, "parentid": "1", "state": "open", "text": "springmvc" [], "id": "7", "parent": true, "parentId": "1", "state": "open", "text": "fastdfs" }, { "checked": false, "children": [], "id": "8", "parent": true, "parentId": "1", "state": "open", "text": "linux" }, { "checked": false, "children": [], "id": "16", "parent": true, "parentid": "1", "état": "ouvrir", "texte": "etc"}], "id": "1", "parent": true, "parentId": "0", "état": "ouvrir", "text": "apprentissage technique"}, {"vérifié": false " "id": "9", "parent": true, "parentid": "2", "état": "ouvert", "texte": "cyclisme"}, {"vérifié": false, "enfants": [], "id": "10", "parent": "Brinking": "2" "," State ":" Open "," SEXT ":" Manger, consommation d'alcool et s'amuser ", {{" "," [], "id": "11", "parent": true, "parentid": "2", "état": "ouvrir", "texte": "apprentissage"}, {"vérifié": false, "enfants": [], "id": "17", "parent": true, "Parentid": "2", "State": "Open", "text": "et" "]"] " "Parentid": "0", "State": "Open", "Text": "Intérêt"}], "ID": "0", "Parent": FAUX, "PALTID": "", "State": "Open", "Text": "About Me"}], "Id": "-1", "Parent": False ".Ce qui précède est le contenu complet de la méthode Java pour produire des données JSON de structure d'arbre en fonction du contenu de la table de base de données. J'espère que tout le monde pourra soutenir wulin.com ~