通过递归展示树状结构
来源:https://blog.csdn.net/qq_42765276/article/details/87933560
使用eclipse springboot2.0.5 jdk1.8
项目结构:
一.准备表结构及对应的表数据
CREATE TABLE `tb_tree` ( `cid` int(11) NOT NULL, `cname` varchar(255) DEFAULT NULL, `pid` int(11) DEFAULT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
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);
二。pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dcsyun</groupId> <artifactId>spring-boot-09-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-09-mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
三。TreeNode.java
public class TreeNode implements Serializable { private static final long serialVersionUID = 1L; private Integer cid; private String cname; private Integer pid; private List<TreeNode> nodes = new ArrayList<TreeNode>(); // getter/setter }
四。TreeMapper.java
@Mapper public interface TreeMapper { @Select("SELECT * FROM tb_tree t WHERE t.cid=#{cid}") public TreeNode getTreeNode(Integer cid); @Select("SELECT * FROM tb_tree t WHERE t.pid=#{pid}") public List<TreeNode> queryTreeNode(Integer pid); }
五.application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/spring_cache spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.configuration.map-underscore-to-camel-case=true logging.level.com.dcsyun.mapper=debug debug=true
六.SpringBoot09MybatisApplicationTests.java
@RunWith(SpringRunner.class) @SpringBootTest public class SpringBoot09MybatisApplicationTests { @Autowired private TreeMapper treeDao; @Test public void contextLoads() throws Exception { TreeNode treeNode = recursiveTree(1); ObjectMapper om = new ObjectMapper(); String str = om.writeValueAsString(treeNode); System.out.println(str); } public TreeNode recursiveTree(int cid) { // 根据cid获取节点对象 TreeNode node = treeDao.getTreeNode(cid); // 查询cid下的所有子节点 List<TreeNode> childTreeNodes = treeDao.queryTreeNode(cid); // 遍历子节点 for(TreeNode child : childTreeNodes){ TreeNode n = recursiveTree(child.getCid()); // 递归 node.getNodes().add(n); } return node; } }