• JAVA 根据数据库表内容生产树结构JSON数据


    1、利用场景

      组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段

    2、构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据)

     1 List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
     2 tests.add(new Test("0", "", "关于本人"));
     3 tests.add(new Test("1", "0", "技术学习"));
     4 tests.add(new Test("2", "0", "兴趣"));
     5 tests.add(new Test("3", "1", "JAVA"));
     6 tests.add(new Test("4", "1", "oracle"));
     7 tests.add(new Test("5", "1", "spring"));
     8 tests.add(new Test("6", "1", "springmvc"));
     9 tests.add(new Test("7", "1", "fastdfs"));
    10 tests.add(new Test("8", "1", "linux"));
    11 tests.add(new Test("9", "2", "骑行"));
    12 tests.add(new Test("10", "2", "吃喝玩乐"));
    13 tests.add(new Test("11", "2", "学习"));
    14 tests.add(new Test("12", "3", "String"));
    15 tests.add(new Test("13", "4", "sql"));
    16 tests.add(new Test("14", "5", "ioc"));
    17 tests.add(new Test("15", "5", "aop"));
    18 tests.add(new Test("16", "1", "等等"));
    19 tests.add(new Test("17", "2", "等等"));
    20 tests.add(new Test("18", "3", "等等"));
    21 tests.add(new Test("19", "4", "等等"));
    22 tests.add(new Test("20", "5", "等等"));

    3、源码

    Tree.java

      1 package pers.kangxu.datautils.bean.tree;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 import java.util.Map;
      6 
      7 import com.alibaba.fastjson.JSON;
      8 
      9 /**
     10  * tree TODO <br>
     11  * 
     12  * @author kangxu2 2017-1-7
     13  * 
     14  */
     15 public class Tree<T> {
     16     /**
     17      * 节点ID
     18      */
     19     private String id;
     20     /**
     21      * 显示节点文本
     22      */
     23     private String text;
     24     /**
     25      * 节点状态,open closed
     26      */
     27     private String state = "open";
     28     /**
     29      * 节点是否被选中 true false
     30      */
     31     private boolean checked = false;
     32     /**
     33      * 节点属性
     34      */
     35     private List<Map<String, Object>> attributes;
     36     /**
     37      * 节点的子节点
     38      */
     39     private List<Tree<T>> children = new ArrayList<Tree<T>>();
     40 
     41     /**
     42      * 父ID
     43      */
     44     private String parentId;
     45     /**
     46      * 是否有父节点
     47      */
     48     private boolean isParent = false;
     49     /**
     50      * 是否有子节点
     51      */
     52     private boolean isChildren = false;
     53 
     54     public String getId() {
     55         return id;
     56     }
     57 
     58     public void setId(String id) {
     59         this.id = id;
     60     }
     61 
     62     public String getText() {
     63         return text;
     64     }
     65 
     66     public void setText(String text) {
     67         this.text = text;
     68     }
     69 
     70     public String getState() {
     71         return state;
     72     }
     73 
     74     public void setState(String state) {
     75         this.state = state;
     76     }
     77 
     78     public boolean isChecked() {
     79         return checked;
     80     }
     81 
     82     public void setChecked(boolean checked) {
     83         this.checked = checked;
     84     }
     85 
     86     public List<Map<String, Object>> getAttributes() {
     87         return attributes;
     88     }
     89 
     90     public void setAttributes(List<Map<String, Object>> attributes) {
     91         this.attributes = attributes;
     92     }
     93 
     94     public List<Tree<T>> getChildren() {
     95         return children;
     96     }
     97 
     98     public void setChildren(List<Tree<T>> children) {
     99         this.children = children;
    100     }
    101 
    102     public boolean isParent() {
    103         return isParent;
    104     }
    105 
    106     public void setParent(boolean isParent) {
    107         this.isParent = isParent;
    108     }
    109 
    110     public boolean isChildren() {
    111         return isChildren;
    112     }
    113 
    114     public void setChildren(boolean isChildren) {
    115         this.isChildren = isChildren;
    116     }
    117 
    118     public String getParentId() {
    119         return parentId;
    120     }
    121 
    122     public void setParentId(String parentId) {
    123         this.parentId = parentId;
    124     }
    125 
    126     public Tree(String id, String text, String state, boolean checked,
    127             List<Map<String, Object>> attributes, List<Tree<T>> children,
    128             boolean isParent, boolean isChildren, String parentID) {
    129         super();
    130         this.id = id;
    131         this.text = text;
    132         this.state = state;
    133         this.checked = checked;
    134         this.attributes = attributes;
    135         this.children = children;
    136         this.isParent = isParent;
    137         this.isChildren = isChildren;
    138         this.parentId = parentID;
    139     }
    140 
    141     public Tree() {
    142         super();
    143     }
    144 
    145     @Override
    146     public String toString() {
    147         
    148         return JSON.toJSONString(this);
    149     }
    150 
    151 }

    BuildTree.java

     1 package pers.kangxu.datautils.common.tree;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import pers.kangxu.datautils.bean.tree.Tree;
     7 
     8 /**
     9  * 构建tree
    10  * TODO
    11  * <br>
    12  * @author kangxu2 2017-1-7
    13  *
    14  */
    15 public class BuildTree {
    16 
    17     /**
    18      * 
    19      * TODO
    20      * <br>
    21      * @author kangxu2 2017-1-7
    22      *
    23      * @param nodes
    24      * @return
    25      */
    26     public static <T> Tree<T> build(List<Tree<T>> nodes) {
    27 
    28         if(nodes == null){
    29             return null;
    30         }
    31         List<Tree<T>> topNodes = new ArrayList<Tree<T>>();
    32 
    33         for (Tree<T> children : nodes) {
    34 
    35             String pid = children.getParentId();
    36             if (pid == null || "".equals(pid)) {
    37                 topNodes.add(children);
    38 
    39                 continue;
    40             }
    41 
    42             for (Tree<T> parent : nodes) {
    43                 String id = parent.getId();
    44                 if (id != null && id.equals(pid)) {
    45                     parent.getChildren().add(children);
    46                     children.setParent(true);
    47                     parent.setChildren(true);
    48                     
    49                     continue;
    50                 }
    51             }
    52 
    53         }
    54 
    55         Tree<T> root = new Tree<T>();
    56         if (topNodes.size() == 1) {
    57             root = topNodes.get(0);
    58         } else {
    59             root.setId("-1");
    60             root.setParentId("");
    61             root.setParent(false);
    62             root.setChildren(true);
    63             root.setChecked(true);
    64             root.setChildren(topNodes);
    65             root.setText("顶级节点");
    66 
    67         }
    68 
    69         return root;
    70     }
    71 
    72 }

    BuildTreeTester.java

     1 package pers.kangxu.datautils.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import pers.kangxu.datautils.bean.tree.Tree;
     7 import pers.kangxu.datautils.common.tree.BuildTree;
     8 
     9 public class BuildTreeTester {
    10 
    11     public static void main(String[] args) {
    12         
    13         
    14         List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
    15         List<Test> tests = new ArrayList<Test>();
    16         tests.add(new Test("0", "", "关于本人"));
    17         tests.add(new Test("1", "0", "技术学习"));
    18         tests.add(new Test("2", "0", "兴趣"));
    19         tests.add(new Test("3", "1", "JAVA"));
    20         tests.add(new Test("4", "1", "oracle"));
    21         tests.add(new Test("5", "1", "spring"));
    22         tests.add(new Test("6", "1", "springmvc"));
    23         tests.add(new Test("7", "1", "fastdfs"));
    24         tests.add(new Test("8", "1", "linux"));
    25         tests.add(new Test("9", "2", "骑行"));
    26         tests.add(new Test("10", "2", "吃喝玩乐"));
    27         tests.add(new Test("11", "2", "学习"));
    28         tests.add(new Test("12", "3", "String"));
    29         tests.add(new Test("13", "4", "sql"));
    30         tests.add(new Test("14", "5", "ioc"));
    31         tests.add(new Test("15", "5", "aop"));
    32         tests.add(new Test("16", "1", "等等"));
    33         tests.add(new Test("17", "2", "等等"));
    34         tests.add(new Test("18", "3", "等等"));
    35         tests.add(new Test("19", "4", "等等"));
    36         tests.add(new Test("20", "5", "等等"));
    37         
    38         for (Test test : tests) {
    39             Tree<Test> tree = new Tree<Test>();
    40             tree.setId(test.getId());
    41             tree.setParentId(test.getPid());
    42             tree.setText(test.getText());
    43             
    44             trees.add(tree);
    45         }
    46 
    47         Tree<Test> t = BuildTree.build(trees);
    48         System.out.println(t);
    49     }
    50 }
    51 
    52 class Test {
    53 
    54     private String id;
    55     private String pid;
    56     private String text;
    57 
    58     public String getId() {
    59         return id;
    60     }
    61 
    62     public void setId(String id) {
    63         this.id = id;
    64     }
    65 
    66     public String getPid() {
    67         return pid;
    68     }
    69 
    70     public void setPid(String pid) {
    71         this.pid = pid;
    72     }
    73 
    74     public String getText() {
    75         return text;
    76     }
    77 
    78     public void setText(String text) {
    79         this.text = text;
    80     }
    81 
    82     public Test(String id, String pid, String text) {
    83         super();
    84         this.id = id;
    85         this.pid = pid;
    86         this.text = text;
    87     }
    88 
    89     public Test() {
    90         super();
    91     }
    92 
    93     @Override
    94     public String toString() {
    95         return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]";
    96     }
    97 
    98 }

    4、运行结果

    JSON数据:

      1 {
      2     "checked":false,
      3     "children":[
      4         {
      5             "checked":false,
      6             "children":[
      7                 {
      8                     "checked":false,
      9                     "children":[
     10                         {
     11                             "checked":false,
     12                             "children":[
     13 
     14                             ],
     15                             "id":"12",
     16                             "parent":true,
     17                             "parentId":"3",
     18                             "state":"open",
     19                             "text":"String"
     20                         },
     21                         {
     22                             "checked":false,
     23                             "children":[
     24 
     25                             ],
     26                             "id":"18",
     27                             "parent":true,
     28                             "parentId":"3",
     29                             "state":"open",
     30                             "text":"等等"
     31                         }
     32                     ],
     33                     "id":"3",
     34                     "parent":true,
     35                     "parentId":"1",
     36                     "state":"open",
     37                     "text":"JAVA"
     38                 },
     39                 {
     40                     "checked":false,
     41                     "children":[
     42                         {
     43                             "checked":false,
     44                             "children":[
     45 
     46                             ],
     47                             "id":"13",
     48                             "parent":true,
     49                             "parentId":"4",
     50                             "state":"open",
     51                             "text":"sql"
     52                         },
     53                         {
     54                             "checked":false,
     55                             "children":[
     56 
     57                             ],
     58                             "id":"19",
     59                             "parent":true,
     60                             "parentId":"4",
     61                             "state":"open",
     62                             "text":"等等"
     63                         }
     64                     ],
     65                     "id":"4",
     66                     "parent":true,
     67                     "parentId":"1",
     68                     "state":"open",
     69                     "text":"oracle"
     70                 },
     71                 {
     72                     "checked":false,
     73                     "children":[
     74                         {
     75                             "checked":false,
     76                             "children":[
     77 
     78                             ],
     79                             "id":"14",
     80                             "parent":true,
     81                             "parentId":"5",
     82                             "state":"open",
     83                             "text":"ioc"
     84                         },
     85                         {
     86                             "checked":false,
     87                             "children":[
     88 
     89                             ],
     90                             "id":"15",
     91                             "parent":true,
     92                             "parentId":"5",
     93                             "state":"open",
     94                             "text":"aop"
     95                         },
     96                         {
     97                             "checked":false,
     98                             "children":[
     99 
    100                             ],
    101                             "id":"20",
    102                             "parent":true,
    103                             "parentId":"5",
    104                             "state":"open",
    105                             "text":"等等"
    106                         }
    107                     ],
    108                     "id":"5",
    109                     "parent":true,
    110                     "parentId":"1",
    111                     "state":"open",
    112                     "text":"spring"
    113                 },
    114                 {
    115                     "checked":false,
    116                     "children":[
    117 
    118                     ],
    119                     "id":"6",
    120                     "parent":true,
    121                     "parentId":"1",
    122                     "state":"open",
    123                     "text":"springmvc"
    124                 },
    125                 {
    126                     "checked":false,
    127                     "children":[
    128 
    129                     ],
    130                     "id":"7",
    131                     "parent":true,
    132                     "parentId":"1",
    133                     "state":"open",
    134                     "text":"fastdfs"
    135                 },
    136                 {
    137                     "checked":false,
    138                     "children":[
    139 
    140                     ],
    141                     "id":"8",
    142                     "parent":true,
    143                     "parentId":"1",
    144                     "state":"open",
    145                     "text":"linux"
    146                 },
    147                 {
    148                     "checked":false,
    149                     "children":[
    150 
    151                     ],
    152                     "id":"16",
    153                     "parent":true,
    154                     "parentId":"1",
    155                     "state":"open",
    156                     "text":"等等"
    157                 }
    158             ],
    159             "id":"1",
    160             "parent":true,
    161             "parentId":"0",
    162             "state":"open",
    163             "text":"技术学习"
    164         },
    165         {
    166             "checked":false,
    167             "children":[
    168                 {
    169                     "checked":false,
    170                     "children":[
    171 
    172                     ],
    173                     "id":"9",
    174                     "parent":true,
    175                     "parentId":"2",
    176                     "state":"open",
    177                     "text":"骑行"
    178                 },
    179                 {
    180                     "checked":false,
    181                     "children":[
    182 
    183                     ],
    184                     "id":"10",
    185                     "parent":true,
    186                     "parentId":"2",
    187                     "state":"open",
    188                     "text":"吃喝玩乐"
    189                 },
    190                 {
    191                     "checked":false,
    192                     "children":[
    193 
    194                     ],
    195                     "id":"11",
    196                     "parent":true,
    197                     "parentId":"2",
    198                     "state":"open",
    199                     "text":"学习"
    200                 },
    201                 {
    202                     "checked":false,
    203                     "children":[
    204 
    205                     ],
    206                     "id":"17",
    207                     "parent":true,
    208                     "parentId":"2",
    209                     "state":"open",
    210                     "text":"等等"
    211                 }
    212             ],
    213             "id":"2",
    214             "parent":true,
    215             "parentId":"0",
    216             "state":"open",
    217             "text":"兴趣"
    218         }
    219     ],
    220     "id":"0",
    221     "parent":false,
    222     "parentId":"",
    223     "state":"open",
    224     "text":"关于本人"
    225 }
    学习无止尽,代码我疯狂
  • 相关阅读:
    vss的ss.ini丢失或损坏导致的vss无法登录错误
    NHibernate各种数据库连接参数文件配置方法说明
    oracle操作语句
    企业微信群聊机器人发送本地图片
    securecrt 或xshell 转发80端口
    SecureCRT 或 XSHELL 转发 X11 图形化GUI
    Windows 创建 .gdbinit 提示必须键入文件名
    vscode C++ 程序 windows
    vscode C++ 程序 windows
    解决source insight 4.0 不识别.cc文件的问题
  • 原文地址:https://www.cnblogs.com/kangxu/p/6260755.html
Copyright © 2020-2023  润新知