1. Cenários de utilização
Árvore da estrutura organizacional, geralmente existe uma tabela de estrutura organizacional, incluindo campos como código (código), pcode (código superior), nome (nome da organização)
2. Construa dados (os seguintes dados não são dados organizacionais, mas puramente dados que eu compunham)
Lista <Árvore <Test>> árvores = new ArrayList <Tree <Test>> (); tests.add (novo teste ("0", "", "Sobre mim")); tests.add (novo teste ("1", "0", "aprendizado técnico"); tests.add (new "2", "" "," "juros); "Java")); tests.add (novo teste ("4", "1", "oracle")); tests.add (novo teste ("5", "1", "spring")); tests.add (new test ("6", "1", "springmvc")); "1", "linux)); tests.add (novo teste (" 9 "," 2 "," ciclismo ")); tests.add (novo teste (" 10 "," 2 "," comer, beber, reproduzir ")); tests.add (new" 12 "," "" "2," 2, "2," Learning ")))); tests.add (" 12 "12", "" "" 2, "2," 2, "Learning"))); "4", "sql")); tests.add (novo teste ("14", "5", "ioc")); tests.add (novo teste ("15", "5", "AOP")); tests.add (novo teste ("16,", "1", "etc.));); "etc")); tests.add (novo teste ("19", "4", "etc")); tests.add (novo teste ("20", "5", "etc"))); tests.add (novo teste ("20", "5," etc "); test.add (novo teste (" 20 "," 5 "," etc "); "etc")); tests.add (novo teste ("19", "4", "etc")); tests.add (novo teste ("20", "5", "etc")); tests.add (novo teste ("20", "5", "etc");3. Código -fonte
Árvore.java
pacote pers.kangxu.datautils.bean.tree; importar java.util.ArrayList; importar java.util.list; importar java.util.map; importar com.alibaba.fastjson.json;/** * Tree <br> * * @author kangxu27-1-1-1-7-7; eu ia; / *** Mostrar texto do nó*/ texto de sequência privada; / ** * status do nó, aberto fechado */ private string state = "aberto"; / ** * Se o nó é selecionado verdadeiro false */ privado booleano verificado = false; / *** atributo do nó*/ Lista privada <map <string, objeto >> atributos; / *** Filhos do nó*/ Lista privada <TREE <T>> CRIANÇAS = NOVO ARRAYLIST <TREE <T>> (); / ** * ID do pai */ String privada parentid; / *** Se existe um nó pai*/ private boolean isParent = false; / *** Se existe um nó filho*/ private booleano ischildren = false; public string getId () {return id; } public void setId (string id) {this.id = id; } public string getText () {return text; } public void setText (text string) {this.text = text; } public string getState () {retornar estado; } public void setState (State String) {this.state = state; } public boolean ischecked () {return verificado; } public void SetChecked (boolean verificado) {this.checked = checked; } Lista pública <map <string, object >> getAttributes () {return Attributes; } public void setAtTributes (list <map <string, object >> atributos) {this.attributes = atributos; } Lista pública <Tree <T>> getChildren () {return Children; } public void setChildren (List <Tree <T>> crianças) {this.Children = Children; } public boolean isParent () {return isParent; } public void setParent (boolean isParent) {this.isparent = isParent; } public boolean ischildren () {return ischildren; } public void setChildren (boolean ischildren) {this.ischildren = ischildren; } public string getParentId () {return parentid; } public void setParentId (String parentId) {this.parentId = parentid; } Árvore pública (ID da string, texto da string, estado da string, check -out boolean, list <map <string, objeto >> Atributos, lista <Tree <T>> Filhos, Isparent boolean, ischildren boolean, string parentid) {super (); this.id = id; this.Text = text; this.state = state; this.checked = verificado; this.attributes = atributos; this.Children = filhos; this.isparent = isParent; this.ischildren = ischildren; this.parentId = parentid; } public árvore () {super (); } @Override public string tostring () {return json.tojSonstring (this); }}BuildTree.java
pacote pers.kangxu.datautils.common.tree; importar java.util.ArrayList; importar java.util.list; importar pers.kangxu.datautils.bean.tree.tree;/** * build * thedo * <br> * @author kangxu27-1-17-1-7-1-7 ** * @author kangxu2 2017-1-7 * * @param nós * @return */ public static <t> árvore <t> build (list <árvore <t>> nós) {if (nós == null) {return null; } List <árvore <T>> topnodes = new ArrayList <Tree <T>> (); para (árvore <T> filhos: nós) {string pid = children.getParentId (); if (pid == null || "" .equals (pid)) {topnodes.add (filhos); continuar; } para (árvore <T> pai: nós) {string id = parent.getId (); if (id! = null && id.equals (pid)) {parent.getChildren (). add (filhos); filhos.setParent (verdadeiro); parent.Setchildren (true); continuar; }}} Árvore <T> root = nova árvore <T> (); if (topnodes.size () == 0) {root = topNodes.get (0); } else {root.setId ("-1"); root.setParentid (""); root.setParent (false); root.Setchildren (true); root.setchecked (true); root.Setchildren (topnodes); root.settext ("nó de nível superior"); } retornar root; }}BuildTreeTester.java
pacote pers.kangxu.datautils.test; importar java.util.ArrayList; importar java.util.list; importar pers.kangxu.datautils.bean.tree.tree; stringtreetter (publicTr.kangxu.datautils.common.Tree.Buildree; Lista <Árvore <Test>> árvores = new ArrayList <Árvore <Test>> (); List <Test> tests = new ArrayList <Test> (); tests.add (novo teste ("0", "", "Sobre mim")); tests.add (novo teste ("1", "0", "aprendizado técnico")); tests.add (novo teste ("2", "0", "interesse")); tests.add (novo teste ("3", "1", "java")); tests.add (novo teste ("4", "1", "Oracle")); tests.add (novo teste ("5", "1", "primavera")); tests.add (novo teste ("6", "1", "springmvc")); tests.add (novo teste ("7", "1", "fastDFS")); tests.add (novo teste ("8", "1", "Linux")); tests.add (novo teste ("9", "2", "ciclismo")); tests.add (novo teste ("10", "2", "comer, beber e se divertir"); tests.add (novo teste ("11", "2", "Learning")); tests.add (novo teste ("12", "3", "string")); tests.add (novo teste ("13", "4", "sql")); tests.add (novo teste ("14", "5", "IOC")); tests.add (novo teste ("15", "5", "AOP")); tests.add (novo teste ("16", "1", "etc")); tests.add (novo teste ("17", "2", "etc")); tests.add (novo teste ("18", "3", "etc")); tests.add (novo teste ("19", "4", "etc")); tests.add (novo teste ("20", "5", "etc")); para (teste de teste: testes) {árvore <Teve> árvore = nova árvore <Test> (); árvore.setId (test.getId ()); árvore.setParentID (test.getpid ()); árvore.settext (test.getText ()); árvores.Add (árvore); } Árvore <Test> t = buildtree.build (árvores); System.out.println (t); }} classe teste {private string id; String private PID; texto privado de string; 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 (text string) {this.text = text; } teste público (string id, string pid, text string) {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. Resultados da operação
Dados JSON:
{ "checked": true, "children": [ { "checked": false, "children": [ { "checked": false, "children": [ { "checked": false, "children": [ { "checked": false, "children": [ { "checked": false, "children": [], "id": "12", "parent": true, "parentId": "3", "state": "open", "text": "String" }, { "checked": false, "children": [], "id": "18", "parent": true, "parentId": "3", "state": "open", "text": "etc" } ], "id": "3", "parent": true, "parentId": "1", "state": "open", "text": "JAVA" }, { "checked": false, "children": [ { "checked": false, "children": [], "id": "13", "parent": true, "parentId": "4", "state": "open", "text": "sql" }, { "checked": false, "children": [], "id": "19", "parent": true, "parentId": "4", "state": "open", "text": "etc"}], "id": "4", "Parent": True, "Parentid": "1", "State": "Open", "Text": "Oracle"}, {"Checked": False, "Children": [{"Check": "Falsed," Children: "[]," Id ":" 14 ", [{" True ":" ":" Falsed ":" Children: "[]," id ":" 14 "," True ":" ":" ParentID ":" FaldID ":" [] [], "ID": "14", ", True": " }, { "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": "spring" }, { "checked": false, "children": [], "id": "6", "parent": true, "parentId": "1", "state": "open", "text": "springmvc" }, { "checked": false, "children": [], "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", "state": "open", "text": "etc" } ], "id": "1", "parent": true, "parentId": "0", "state": "open", "text": "Technical Learning" }, { "checked": false, "children": [ { "checked": false, "children": [], "ID": "9", "Parent": True, "ParentId": "2", "State": "Open", "Text": "Cycling"}, {"Checked": False, "Children": [], "Id": "10,": "Comenda": "Diretor": "Diretor", ",", ",", ",", ",", ",", ",", ",", ",", ",", ",", ",", "o que o pai": ",", ",", ",", "o que o pare": ",", ",", ",", "," o que o pare do seu [], "id": "11", "parent": true, "parentId": "2", "state": "open", "text": "learning" }, { "checked": false, "children": [], "id": "17", "parent": true, "parentId": "2", "state": "open", "text": "etc" } ], "id": "2", "parent": true, "parentId": "0", "state": "open", "text": "interest" } ], "id": "0", "parent": false, "parentId": "", "state": "open", "text": "about me" } ], "id": "-1", "parent": false, "parentId": "", "state": "open", "text": "Top Node"}O acima exposto é o conteúdo completo do método Java para produzir dados JSON de estrutura de árvores com base no conteúdo da tabela de banco de dados. Espero que todos possam apoiar o wulin.com ~