Java目录树的创建与获取

文件目录路径

您可能是在问有关文件路径的区别。在 Windows 和 Unix/Linux(包括 macOS)系统中,文件路径表示方式存在一些区别:

  1. 路径分隔符

    • Windows 使用反斜杠(\)作为路径分隔符,例如 C:\Users\Username\Documents\File.txt
    • Unix/Linux 使用正斜杠(/)作为路径分隔符,例如 /home/username/documents/file.txt
  2. 根目录表示

    • Windows 中的根目录表示为盘符(C:\、D:\ 等),例如 C:\.
    • Unix/Linux 中的根目录表示为 /
  3. 特殊目录表示

    • Windows 中,当前目录表示为 .,上一级目录表示为 ..

逻辑代码

    /**
     * @描述 应用目录树添加
     * @作者 梁伟浩
     * @日期 2024/3/29 15:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveApplyContent(ApplyDirContentRequest request) {
        BiProjectDir projectDir = new BiProjectDir();
        BeanUtil.copyProperties(request, projectDir);
        projectDir.setCreateUser(request.getUserId());
        projectDir.setCreateTime(new Date());
        projectDir.setUpdateUser(request.getUserId());
        projectDir.setUpdateTime(new Date());
        // 参数没传父级ID,则进入方法查询是否有父级目录,有则取其ID作为父ID无则创建根节点配置
        if (ObjectUtil.isEmpty(request.getParentId())) {
            BiProjectDir rootContent = this.getRootContent(request.getProjectId());
            //获取父级ID
            projectDir.setParentId(rootContent.getId());
        }else {
            //获取当前目录排序,获取最大值+1
            Integer sort = biProjectDirMapper.selectMaxSort(request.getParentId());
            projectDir.setSort(sort == null ? 1 : ++sort);
        }
        biProjectDirMapper.insert(projectDir);
        return R.data(projectDir);
    }


    private BiProjectDir getRootContent(Long projectId) {
      //查出同一项目下的所有目录与文件
        BiProjectDir rootContent = biProjectDirMapper.selectOne(new LambdaQueryWrapper<BiProjectDir>().eq(BiProjectDir::getProjectId,projectId).eq(BiProjectDir::getName,"根节点配置"));
        if (rootContent == null) {
            // 如果没有根节点配置,则自动新建根节点配置
            rootContent = new BiProjectDir();
            rootContent.setName("根节点配置");
            rootContent.setProjectId(projectId);
            rootContent.setSort(1);
            biProjectDirMapper.insert(rootContent);
        }
        return rootContent;
    }

    /**
     * @param projectId 项目ID
     * @描述 获取应用目录树
     * @作者 梁伟浩
     * @日期 2024/3/30 14:20 星期六
     */
    @Override
    public R<List<ApplyContentResponse>> getApplyContent(Long projectId) {
        List<ApplyContentResponse> applyContentResponses = new ArrayList<>();
        List<BiProjectDir> biProjectDirs = biProjectDirMapper.selectList(new LambdaQueryWrapper<BiProjectDir>().eq(BiProjectDir::getProjectId, projectId));
        if (!biProjectDirs.isEmpty()){
            biProjectDirs.forEach(a->{
                ApplyContentResponse response = new ApplyContentResponse();
                BeanUtil.copyProperties(a, response);
                applyContentResponses.add(response);
            });
            //构建目录树
            List<ApplyContentResponse> applyContentResponseList = this.recursionMethod(applyContentResponses);
            System.out.println(applyContentResponseList);
            return R.data(applyContentResponseList);
        }
        return null;
    }

查询目录最大排序

    /**
     * 根据父级ID查询出所属目录的最大排序
     * @param parentId
     * @作者 梁伟浩
     * @日期 2024/4/15 14:43 星期一
     * @return Integer
     */
    Integer selectMaxSort(@Param("parentId") Long parentId);

   <select id="selectMaxSort" resultType="java.lang.Integer" parameterType="java.lang.Long">
    <if test="parentId != null">
      SELECT MAX(sort) AS max_sort
      FROM bi_project_dir
      WHERE parent_id = #{parentId}
    </if>
  </select>

递归方法转换成树形结构

   /**
     * 递归方法转换成树形结构
     * @param treeList
     * @author 梁伟浩
     * @return
     */
    public  List<ApplyContentResponse> recursionMethod(List<ApplyContentResponse> treeList) {
        List<ApplyContentResponse> trees = new ArrayList<>();
        for (ApplyContentResponse tree : treeList) {
            // 找出父节点
            if (Objects.isNull(tree.getParentId())) {
                // 调用递归方法填充子节点列表
                trees.add(findChildren(tree, treeList));
            }
        }
        return trees;
    }

    /**
     * 递归方法
     * @param tree 父节点对象
     * @param treeList 所有的List
     * @author 梁伟浩
     * @return
     */
    public static ApplyContentResponse findChildren(ApplyContentResponse tree, List<ApplyContentResponse> treeList) {
        for (ApplyContentResponse node : treeList) {
            if (tree.getId().equals(node.getParentId())) {
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<>());
                }
                // 递归调用自身
                tree.getChildren().add(findChildren(node, treeList));
            }
        }
        return tree;
    }

实体

package com.guodi.bi.dmp.entiy;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Date;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * 项目专题目录图层表
 */
@Data
@ApiModel(value = "项目专题目录图层表", description = "项目专题目录图层表")
@TableName("bi_project_dir")
public class BiProjectDir {
    /**
     * 主键ID
     */
    @ApiModelProperty(value = "主键ID")
    private Long id;

    /**
     * 名称
     */
    @ApiModelProperty(value = "名称")
    private String name;

    /**
     * 上级ID
     */
    @ApiModelProperty(value = "上级ID")
    private Long parentId;


    /**
     * 备注
     */
    @ApiModelProperty(value = "备注")
    private String remark;

    /**
     * 目录/图层(1.目录,2图层)
     */
    @ApiModelProperty(value = "目录/图层(1.目录,2图层)")
    private Integer type;

    /**
     * 显示顺序
     */
    @ApiModelProperty(value = "显示顺序")
    private Integer sort;

    /**
     * 项目ID
     */
    @ApiModelProperty(value = "项目ID")
    private Long projectId;

    /**
     * 项目数据页面ID
     */
    @ApiModelProperty(value = "项目数据页面ID")
    private Long projectDataId;

    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人")
    private Long createUser;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 更新人
     */
    @ApiModelProperty(value = "更新人")
    private Long updateUser;

    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 是否已删除
     */
    @TableLogic
    @ApiModelProperty(value = "是否已删除")
    private Integer isDeleted = 0;
}

响应实体

package com.guodi.bi.dmp.vo.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.guodi.bi.dmp.entiy.BiProjectDir;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * @author 梁伟浩
 * @description 应用目录树返回实体
 * @date 2024/3/30 14:15
 * @study 星期六
 */
@Data
public class ApplyContentResponse  extends BiProjectDir {

    /**
     * 子孙节点
     */
    @ApiModelProperty(value = "子节点")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<BiProjectDir> children;

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java中的战斗机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值