表结构
SQL结构和数据
DROP TABLE IF EXISTS `demo`; CREATE TABLE `demo` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pid` int(11) NOT NULL, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO `demo` VALUES ('1', '0', 'A'); INSERT INTO `demo` VALUES ('2', '1', 'B'); INSERT INTO `demo` VALUES ('3', '1', 'C'); INSERT INTO `demo` VALUES ('4', '2', 'D'); INSERT INTO `demo` VALUES ('5', '2', 'E'); INSERT INTO `demo` VALUES ('6', '3', 'F'); INSERT INTO `demo` VALUES ('7', '3', 'G');
Pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency>
application.properties文件
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tree spring.datasource.username=root spring.datasource.password=1234 spring.datasource.driver-class-name=com.mysql.jdbc.Driver TreeMapper @Mapper public interface TreeMapper { @Select("select * from demo") List<Tree> getAll(); }
对应数据库的Tree实体类
public class Tree { private Integer id; private Integer pid; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Tree{" + "id=" + id + ", pid=" + pid + ", name='" + name + '\'' + '}'; } }
NewTree,定义返回给前段的树类型,根据业务不同自行修改(这里为了简单起见,只修改了id和pid的类型)
public class NewTree { private String id; private String pid; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Tree{" + "id=" + id + ", pid=" + pid + ", name='" + name + '\'' + '}'; } }
MenuTreeUtil,递归构造树形结构
@Component public class MenuTreeUtil { //已经被buildTree的list集合 private List<NewTree> menuCommon; //返回给前端的NewTree List集合 private List<Object> list = new ArrayList<Object>(); public List<Object> menuList(List<NewTree> menu){ this.menuCommon = menu; // 通过遍历menu,找到父节点为0的节点,它是顶级父节点 // 然后调用menuChild,递归遍历所有子节点 for (NewTree x : menu) { Map<String,Object> mapArr = new LinkedHashMap<String, Object>(); if("0".equals(x.getPid())){ mapArr.put("id", x.getId()); mapArr.put("name", x.getName()); mapArr.put("pid", x.getPid()); //遍历开始 mapArr.put("childList", menuChild(x.getId())); list.add(mapArr); } } return list; } private List<?> menuChild(String id){ List<Object> lists = new ArrayList<Object>(); //继续遍历menu for(NewTree a:menuCommon){ Map<String,Object> childArray = new LinkedHashMap<String, Object>(); //找到父ID等于父节点ID的子节点 if(a.getPid().equals(id)){ childArray.put("id", a.getId()); childArray.put("name", a.getName()); childArray.put("pid", a.getPid()); //向下递归 childArray.put("childList", menuChild(a.getId())); lists.add(childArray); } } return lists; } }
TreeController
@Controller public class TreeController { @Resource TreeMapper treeMapper; @Resource MenuTreeUtil menuTreeUtil; @RequestMapping(value = "/menuList",method = {RequestMethod.GET}) @ResponseBody public String getTree() throws Exception { List<NewTree> list = buildTree(treeMapper.getAll()); List<Object> menuList = menuTreeUtil.menuList(list); System.out.println(menuList); return "YES"; } //将实体类,变成标准的树结构,即NewTree类型 private List<NewTree> buildTree(List<Tree> list){ List<NewTree> newTrees = new LinkedList<>(); for(Tree tree:list){ NewTree newTree = new NewTree(); newTree.setId(String.valueOf(tree.getId())); newTree.setPid(String.valueOf(tree.getPid())); newTree.setName(tree.getName()); newTrees.add(newTree); } return newTrees; } }
启动项目,并在地址栏输入如下地址即可
结果展示(JSON在线解析JSON在线 | JSON解析格式化—SO JSON在线工具)
[{ id = 1, name = A, pid = 0, childList = [{ id = 2, name = B, pid = 1, childList = [{ id = 4, name = D, pid = 2, childList = [] }, { id = 5, name = E, pid = 2, childList = [] }] }, { id = 3, name = C, pid = 1, childList = [{ id = 6, name = F, pid = 3, childList = [] }, { id = 7, name = G, pid = 3, childList = [] }] }] }]
参考 https://www.jb51.net/article/125076.htm