java实现多层级目录树详解

简介: java实现多层级目录树详解

一,引言

在开发中,经常遇到前端需要实现一个多层级的目录树,那么后端就需要根据这种结构返回对应的数据,因此在这里记录一下本人在开发中是如何实现这个多层级的目录树。

二,建表建库

在建表时,需要注意的是一定要一个Pid和当前id,这样用于实现这个子级和父级的关联。建表语句如下

CREATE TABLE `site` (  
  `id` bigint(20) NOT NULL AUTO_INCREMENT, 
  `pid` bigint(20) DEFAULT NULL COMMENT '父id', 
  `p_name` varchar(50) DEFAULT NULL COMMENT '父名称', 
  `current_name` varchar(50) DEFAULT NULL COMMENT '当前层级名称', 
  `created_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', 
  `updated_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', 
  `is_deleted` tinyint(4) DEFAULT NULL COMMENT '是否删除', 
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;

可以参考一下我的,也可以自己插入一些数据。接下来插入数据

insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (0,'顶级','江西',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (0,'顶级','广东',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (0,'顶级','湖南',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (0,'顶级','河北',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (1,'江西','南昌',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (1,'江西','赣州',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (1,'江西','九江',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (1,'江西','佛山',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (2,'广东','广州',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (2,'广东','深圳',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (2,'广东','佛山',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (2,'广东','东莞',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (3,'湖南','长沙',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (3,'湖南','湘潭',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (3,'湖南','岳阳',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (4,'河北','唐山',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (4,'河北','石家庄',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (4,'河北','保定',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (10,'深圳',,'南山',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (10,'深圳',,'福田',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (10,'深圳',,'宝安',,now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (10,'深圳',,'龙岗',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (10,'深圳',,'龙华',now(),now(),0);
insert into site (pid,p_name,current_name,created_time,updated_time,is_deleted) VALUES (10,'深圳',,'光明',now(),now(),0);

三,原理

就是利用递归思想,将所有的数据遍历,然后是否有其他结点的Pid为当前结点的id,因此这个数据库的表一定要有一个pid。其实就是类似于两个嵌套的for循环,一个用于遍历,一个用于查找。如果查找成功,那么将那个结点做为当前结点的子节点

for(int i = 0; i < list.size; i++){
  for(int j = 0; j < list.size; j++){
    ...
  }
}

就是将之前在集合中全部零散的结点,变成一棵树状型的树。然后这个1,4结点就是最上面的顶层结点,其他结点依次遍历加入到这两棵子树的后面,最后将这两棵树加入到一个集合里面,就变成了一棵大树。也可以参考一下后面的代码,将这个原理再理解一下。

四,代码实现

这里主要使用springboot项目,依赖和数据库连接之类的暂时不讲。可以参考一下我之前写的这篇https://blog.csdn.net/zhenghuishengq/article/details/109510128?spm=1001.2014.3001.5502


1,实体类

/**
 * @author zhenghuisheng
 * @date : 2022/10/8
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Site implements Serializable {
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    @JsonSerialize(using = ToStringSerializer.class)
    public Long id;
    @JsonSerialize(using = ToStringSerializer.class)
    public Long pid;
    public String pname;
    public String currentName;
    public Date createdTime;
    public Date updatedTime;
    public Integer isDeleted;
}

2、dto类

对应上面的实体类,并在里面加入一个siteDtoList字段,用于保存当前结点的子集。

@Data
public class SiteDto implements Serializable {
    private static final long serialVersionUID = 1L;
    //用户id
    public Long id;
    //父id
    public Long pid;
    //父级name
    public String pname;
    //当前名称
    public String currentName;
    //创建时间
    public String createdTime;
    //更新时间
    public String updatedTime;
    //是否删除
    public Integer isDeleted;
    //子集,用于存储当前目录下面的全部子集
    public List<SiteDto> siteDtoList;
}

3,mybatis里面的sql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.SiteMapper">
    <resultMap id="baseMap" type="com.example.demo.pojo.Site">
        <id column="id" property="id"></id>
        <result column="pid" property="pid"></result>
        <result column="p_name" property="pname"></result>
        <result column="current_name" property="currentName"></result>
        <result column="created_time" property="createdTime"></result>
        <result column="updated_time" property="updatedTime"></result>
        <result column="is_deleted" property="isDeleted"></result>
    </resultMap>
    <select id="queryAll" resultMap="baseMap" resultType="com.example.demo.pojo.Site">
        select * from site where is_deleted = 0;
    </select>
</mapper>

4,Mapper类

/**
 * @author zhenghuisheng
 * @date : 2022/10/8
 */
@Repository
@Mapper
public interface SiteMapper {
    List<Site> queryAll();
}

5,service类

把这个看懂就基本上没问题了

/**
 * @author zhenghuisheng
 * @date : 2022/10/8
 */
@Service
public class SiteService {
    @Autowired
    private SiteMapper siteMapper;
    public List<SiteDto> queryAll(){
        List<SiteDto> datas = new ArrayList<>();
        //获取全部数据
        List<Site> siteList = siteMapper.queryAll();
        if (siteList != null){
            //将这个数据赋值到dto里面
            List<SiteDto> siteDtoList = siteList.stream().map(site -> {
                SiteDto siteDto = new SiteDto();
                BeanUtils.copyProperties(site,siteDto);
                //时间格式化
                siteDto.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(site.createdTime));
                siteDto.setUpdatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(site.updatedTime));
                return siteDto;
            }).collect(Collectors.toList());
            //遍历全部的数据,利用递归思想,获取全部的子集
            siteDtoList.forEach(e ->{
                //判断当前id是否为其他数据的父id
                List<SiteDto> childrenList = getChildrenList(e.getId(), siteDtoList);
                //设置子集,如果到了最后一级,那么直接设置为null,不展示这个属性即可
                e.setSiteDtoList(childrenList != null ? childrenList : null);
            });
            //获取所有的顶点数据,即最上层数据,该数据的pid为0
            //注意这个pid的数据类型,如果数据库为varchar则equals("0") 整型则为equals(0)
            List<SiteDto> siteDtoParents = siteDtoList.stream().filter(t -> t.getPid().equals(0L)).collect(Collectors.toList());
            datas.addAll(siteDtoParents);
        }
        return datas;
    }
    //获取全部的子集合
    public static List<SiteDto> getChildrenList(String id,List<SiteDto> list){
        //便利全部数据,将需要的数据过滤出来
        return list.stream().filter(t-> t.getPid().equals(id)).collect(Collectors.toList());
    }
}

7,Contrller类测试

/**
 * @author zhenghuisheng
 * @date : 2022/10/8
 */
@RestController
public class SiteController {
    @Autowired
    private SiteService siteService;
    @RequestMapping("/getAllData")
    public List<SiteDto> getAllData(){
        return siteService.queryAll();
    }
}

用postman测试一下,结果就出来了

这样就大功告成了!!

相关文章
|
4月前
|
存储 Java
1038. 从二叉搜索树到更大和树 --力扣 --JAVA
给定一个二叉搜索树 root (BST),请将它的每个节点的值替换成树中大于或者等于该节点值的所有节点值之和。 提醒一下, 二叉搜索树 满足下列约束条件: 节点的左子树仅包含键 小于 节点键的节点。 节点的右子树仅包含键 大于 节点键的节点。 左右子树也必须是二叉搜索树。
38 1
|
6月前
|
Java
java读取本地目录的文件转换为list
java读取本地目录的文件转换为list
93 0
|
7月前
|
JSON Java 数据格式
Java读取resource目录下的json文件
Java读取resource目录下的json文件
471 0
|
4月前
|
C++ Java 容器
【Java每日一练】总目录(2023.3.11~5.18)共69篇
【Java每日一练】总目录(2023.3.11~5.18)共69篇
166 0
【Java每日一练】总目录(2023.3.11~5.18)共69篇
|
8天前
|
SQL Java 关系型数据库
java 递归返回树形组织结构(附带树形菜单的搜索)
java 递归返回树形组织结构(附带树形菜单的搜索)
11 0
|
8天前
|
存储 人工智能 Java
Java 构建树型结构
Java 构建树型结构
|
3月前
|
Java
AVL树(Java)
AVL树(Java)
30 0
|
3月前
|
Java
Java另一棵树的子树
Java另一棵树的子树
24 0
|
4月前
|
Java
数据结构 AVL树概念以及实现插入的功能(含Java代码实现)
数据结构 AVL树概念以及实现插入的功能(含Java代码实现)
55 0
|
5月前
|
存储 算法 Java
java树和图相关的算法:二叉树遍历、深度优先搜索、广度优先搜索等
树和图相关的算法:二叉树遍历、深度优先搜索、广度优先搜索等
37 0