使用递归算法结合数据库解析成Java树形结构的代码解析

时间:2022-08-28 21:31:03

1、准备表结构及对应的表数据

a、表结构:

?
1
2
3
4
5
6
create table TB_TREE
(
CID NUMBER not null,
CNAME VARCHAR2(50),
PID NUMBER //父节点
)

b、表数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0);
insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1);
insert into tb_tree (CID, CNAME, PID) values (3, '广东省', 1);
insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);
insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 3);
insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3);
insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 5);
insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 5);
insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 6);
insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 6);
insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 2);
insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 4);

2、TreeNode对象,对应tb_tree

?
1
2
3
4
5
6
7
8
9
public class TreeNode implements Serializable {
private Integer cid;
private String cname;
private Integer pid;
private List nodes = new ArrayList();
public TreeNode() {
}
//getter、setter省略
}

3、测试数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class TreeNodeTest {
@Test
public void loadTree() throws Exception{
System.out.println(JsonUtils.javaToJson(recursiveTree(1)));
}
/**
*
* @param cid
* @return
* @author jiqinlin
*/
public TreeNode recursiveTree(int cid) {
//根据cid获取节点对象(SELECT * FROM tb_tree t WHERE t.cid=?)
TreeNode node = personService.getreeNode(cid);
//查询cid下的所有子节点(SELECT * FROM tb_tree t WHERE t.pid=?)
List childTreeNodes = personService.queryTreeNode(cid);
//遍历子节点
for(TreeNode child : childTreeNodes){
TreeNode n = recursiveTree(child.getCid()); //递归
node.getNodes().add(n);
}
return node;
}
}

输出的json格式如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
  "cid": 1,
  "nodes": [
    {
      "cid": 2,
      "nodes": [
        {
          "cid": 11,
          "nodes": [
          ],
          "cname": "密云县",
          "pid": 2
        }
      ],
      "cname": "北京市",
      "pid": 1
    },
    {
      "cid": 3,
      "nodes": [
        {
          "cid": 5,
          "nodes": [
            {
              "cid": 7,
              "nodes": [
              ],
              "cname": "海珠区",
              "pid": 5
            },
            {
              "cid": 8,
              "nodes": [
              ],
              "cname": "天河区",
              "pid": 5
            }
          ],
          "cname": "广州市",
          "pid": 3
        },
        {
          "cid": 6,
          "nodes": [
            {
              "cid": 9,
              "nodes": [
              ],
              "cname": "福田区",
              "pid": 6
            },
            {
              "cid": 10,
              "nodes": [
              ],
              "cname": "南山区",
              "pid": 6
            }
          ],
          "cname": "深圳市",
          "pid": 3
        }
      ],
      "cname": "广东省",
      "pid": 1
    },
    {
      "cid": 4,
      "nodes": [
        {
          "cid": 12,
          "nodes": [
          ],
          "cname": "浦东",
          "pid": 4
        }
      ],
      "cname": "上海市",
      "pid": 1
    }
  ],
  "cname": "中国",
  "pid": 0
}

总结

以上所述是小编给大家介绍的使用递归算法结合数据库解析成Java树形结构的代码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/linjiqin/archive/2013/06/21/3148066.html