查看: 653|回复: 0

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

发表于 2017-10-2 07:51:44
尚学堂AD

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

a、表结构:

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

b、表数据:

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

2、TreeNode对象,对应tb_tree

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

3、测试数据

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

输出的json格式如下:

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

总结

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



回复

使用道具 举报