spring boot 树状目录(效率太低不建议使用,推荐使用HutoolUtil中的TreeUtil) 您所在的位置:网站首页 element 上传文件夹递归多层级目录 spring boot 树状目录(效率太低不建议使用,推荐使用HutoolUtil中的TreeUtil)

spring boot 树状目录(效率太低不建议使用,推荐使用HutoolUtil中的TreeUtil)

2024-07-03 13:27| 来源: 网络整理| 查看: 265

spring boot 多层级目录查询 表结构

id, name, pid(父id,为0说明是顶层目录), status(状态,1是显示,0是隐藏)

关于理解

使用 mybatis 的 resultMap 和 collection 组合查询,先查询所有顶层目录,将结果扔给resultMap处理,通过collection去调用pid=id的子层级查询,再将这个查询的结果扔给resultMap处理,然后再通过collection去调用pid=id的子层级查询…形成一个递归一样的形式

流程 数据库表类需要给前端的数据结构Vo类mapper接口和实现类xml的sql代码Service接口和ServiceImpl实现类Controller类,接口 实现

步骤一:数据库表类:AClothClass.java

import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class AClothClass { private Integer id; private String name; private Integer pid; private Integer status; }

步骤二:需要给前端的数据结构Vo类:AClothClassVo.java:

import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.List; @Data @NoArgsConstructor @AllArgsConstructor public class AClothClassVo { private Integer id; private String name; private Integer pid; private Integer status; // 子目录列表 private List treeNode; }

步骤三:mapper接口和实现类:AClothClassMapper.java

import com.example.springboot01.vo.AClothClassVo; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface AClothClassMapper { // 查询 List getNodeTree(); }

步骤四:xml的sql代码:AClothClassMapper.xml:

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > SELECT * FROM acloth_class WHERE pid = #{id} AND status = 1 SELECT * FROM acloth_class WHERE pid = 0 AND status = 1

步骤五:Service接口和ServiceImpl实现类: AClothClassService:

import com.example.springboot01.vo.AClothClassVo; import java.util.List; public interface AClothClassService { List getNodeTree(); }

AClothClassServiceImpl.java:

import com.example.springboot01.mapper.AClothClassMapper; import com.example.springboot01.vo.AClothClassVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class AClothClassServiceImpl implements AClothClassService{ @Autowired AClothClassMapper aClothClassMapper; @Override public List getNodeTree() { // 调用数据库查询结果 return aClothClassMapper.getNodeTree(); } }

步骤六:Controller类,接口:AClothClassController.java:

import com.example.springboot01.ret.RetResponse; import com.example.springboot01.ret.RetResult; import com.example.springboot01.service.AClothClassServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/tree") public class AClothClassController { @Autowired AClothClassServiceImpl aClothClassServiceImpl; @GetMapping(value = "/get") public RetResult getTree(){ return RetResponse.success(aClothClassServiceImpl.getNodeTree()); } }

调用 http://127.0.0.1:8080/tree/get 会得到结果:

{ "code": 200, "msg": "success", "data": [ { "id": 1, "name": "顶级目录一", "pid": 0, "status": 1, "treeNode": [ { "id": 4, "name": "二级目录一", "pid": 1, "status": 1, "treeNode": [ { "id": 5, "name": "三级目录一", "pid": 4, "status": 1, "treeNode": [ { "id": 6, "name": "四级目录一", "pid": 5, "status": 1, "treeNode": [] } ] } ] } ] }, { "id": 2, "name": "顶级目录二", "pid": 0, "status": 1, "treeNode": [ { "id": 7, "name": "二级目录二", "pid": 2, "status": 1, "treeNode": [ { "id": 8, "name": "三级目录二", "pid": 7, "status": 1, "treeNode": [] } ] } ] }, { "id": 3, "name": "顶级目录三", "pid": 0, "status": 1, "treeNode": [ { "id": 9, "name": "二级目录三", "pid": 3, "status": 1, "treeNode": [] } ] } ] }

上面的sql会像递归一样一层一层下去找,效率不是很高,如果目录多,层级也深的话效率更低

使用 HutoolUtil 中的 TreeUtil 实现树状结构:

import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.tree.Tree; import cn.hutool.core.lang.tree.TreeNodeConfig; import cn.hutool.core.lang.tree.TreeUtil; import com.alibaba.fastjson.JSON; import com.example.springboot01.vo.AClothClassVo; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest public class HelloApplicationTests { public static void main(String[] args) { //AClothClassVo 的字段:id, name, pid(上级目录id), status List lists = CollUtil.newArrayList(); lists.add(new AClothClassVo(1, "顶级目录1", 0, 1)); lists.add(new AClothClassVo(2, "顶级目录2", 0, 1)); lists.add(new AClothClassVo(3, "顶级目录3", 0, 1)); lists.add(new AClothClassVo(4, "二级目录4", 1, 1)); lists.add(new AClothClassVo(5, "三级目录5", 4, 1)); lists.add(new AClothClassVo(6, "四级目录6", 5, 1)); lists.add(new AClothClassVo(7, "二级目录7", 2, 1)); lists.add(new AClothClassVo(8, "二级目录8", 2, 1)); lists.add(new AClothClassVo(9, "三级目录9", 4, 1)); //配置 TreeNodeConfig treeNodeConfig = new TreeNodeConfig(); // 自定义属性名 都要默认值的 // 排序字段,这个字段不能是null,不然会报错,默认最好是数字 // treeNodeConfig.setWeightKey("order"); // 父级id字段 treeNodeConfig.setIdKey("pid"); // 最大递归深度 // treeNodeConfig.setDeep(3); //转换器 List treeNodes = TreeUtil.build(lists, "0", treeNodeConfig, (treeNode, tree) -> { tree.setId(treeNode.getId().toString()); tree.setParentId(treeNode.getPid().toString()); // tree.setWeight(treeNode.getWeight()); tree.setName(treeNode.getName()); tree.putExtra("status", treeNode.getStatus()); }); System.out.println(JSON.toJSONString(treeNodes)); } }

结果:

[ { "pid":"1", "parentId":"0", "name":"顶级目录1", "status":1, "children":[ { "pid":"4", "parentId":"1", "name":"二级目录4", "status":1, "children":[ { "pid":"5", "parentId":"4", "name":"三级目录5", "status":1, "children":[ { "pid":"6", "parentId":"5", "name":"四级目录6", "status":1 } ] }, { "pid":"9", "parentId":"4", "name":"三级目录9", "status":1 } ] } ] }, { "pid":"2", "parentId":"0", "name":"顶级目录2", "status":1, "children":[ { "pid":"7", "parentId":"2", "name":"二级目录7", "status":1 }, { "pid":"8", "parentId":"2", "name":"二级目录8", "status":1 } ] }, { "pid":"3", "parentId":"0", "name":"顶级目录3", "status":1 } ]


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有