


How to use recursive algorithms and code analysis to parse the database into a Java tree structure
This article mainly introduces the relevant information of code parsing using recursive algorithm combined with database parsing into Java tree structure. Friends in need can refer to the following
1. Prepare table structure And the corresponding table data
a, table structure:
create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父节点 )
b, table data:
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 object, corresponding to tb_tree
public class TreeNode implements Serializable { private Integer cid; private String cname; private Integer pid; private List nodes = new ArrayList(); public TreeNode() { } //getter、setter省略 }
3. Test data
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; } }
The output json format is as follows:
{ "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 }
Summary
The above is the detailed content of How to use recursive algorithms and code analysis to parse the database into a Java tree structure. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor