通过postgresql的Ltree字段类型实现目录结构的基本操作

postgresqlLtree_0">通过postgresqlLtree字段类型实现目录结构的基本操作

将这种具有目录结构的excel表存储到数据库中,可以采用树型结构存储[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZLXAzpxj-1691660171264)(C:\Users\20745\AppData\Roaming\Typora\typora-user-images\image-20230810172124733.png)]

DROP TABLE IF EXISTS "public"."directory_tree";
CREATE TABLE "public"."directory_tree" (
  "id" varchar(100) COLLATE "pg_catalog"."default",
  "path" "public"."ltree",
  "name" varchar(100) COLLATE "pg_catalog"."default" NOT NULL,
  "description" text COLLATE "pg_catalog"."default",
  "updated_at" timestamp(6) DEFAULT now(),
  "created_at" timestamp(6) DEFAULT now()
)
;

-- ----------------------------
-- Records of directory_tree
-- ----------------------------
INSERT INTO "public"."directory_tree" VALUES ('04e19944aa1d3d8bc13971b4488a4e0d', '04e19944aa1d3d8bc13971b4488a4e0d', 'root', 'root', '2023-08-09 02:11:35.145821', '2023-08-09 02:11:35.145821');

上面是建一张表,并且插入一条根节点。这里我们的id是mybatisPuls提供的UUID,并且我们的path字段采用祖id+爷id+父id+子id的结构。这是处理excel表格的工具类

package com.cdcas.utils;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author jiao xn
 * @date 2023/4/20 22:44
 * @description
 */
@Component
public class ExcelUtil {
    /**
     * 根据文件地址,读取指定 Excel 文件的内容,并以对象数组的方式返回
     *
     * @param excelFilePath Excel 文件地址
     * @param sheetIndex 指定 Sheet 的索引值,从 0 开始
     * @param startLine 开始读取的行:从0开始
     * @param tailLine 去除最后读取的行
     * @return Excel 文件内容,对象数组
     */
    public List<Map<String, String>> readExcelFile(String excelFilePath, Integer sheetIndex, Integer startLine
            , Integer tailLine) {
        Workbook workbook = this.generateWorkbook(excelFilePath);
        return this.readExcelSheetToObject(workbook, sheetIndex, startLine, tailLine);
    }

    /**
     * 从 MultipartFile 中读取 Excel 文件的内容,并以对象数组的方式返回
     *
     * @param multipartFile MultipartFile 对象,一般是从前端接收
     * @param sheetIndex 指定 Sheet 的索引值,从 0 开始
     * @param startLine 开始读取的行:从0开始
     * @param tailLine 去除最后读取的行
     * @return Excel 文件内容,对象数组
     */
    public List<Map<String, String>> readExcelFile(MultipartFile multipartFile, Integer sheetIndex, Integer startLine
            , Integer tailLine) {
        Workbook workbook = this.generateWorkbook(multipartFile);
        return this.readExcelSheetToObject(workbook, sheetIndex, startLine, tailLine);
    }

    /**
     * 生成 Workbook 对象
     *
     * @param excelFilePath Excel 文件路径
     * @return Workbook 对象,允许为空
     */
    private Workbook generateWorkbook(String excelFilePath) {
        Workbook workbook;

        try {
            File excelFile = new File(excelFilePath);
            workbook = WorkbookFactory.create(excelFile);
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return workbook;
    }
    /**
     * 生成 Workbook 对象
     *
     * @param multipartFile MultipartFile 对象
     * @return Workbook 对象
     */
    private Workbook generateWorkbook(MultipartFile multipartFile) {
        Workbook workbook;

        try {
            workbook = WorkbookFactory.create(multipartFile.getInputStream());
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return workbook;
    }

    /**
     * 读取指定 Sheet 中的数据
     *
     * @param workbook Workbook 对象
     * @param sheetIndex 指定 Sheet 的索引值,从 0 开始
     * @param startLine 开始读取的行:从0开始
     * @param tailLine 去除最后读取的行
     * @return 指定 Sheet 的内容
     */
    private List<Map<String, String>> readExcelSheetToObject(Workbook workbook,
                                                             Integer sheetIndex, Integer startLine, Integer tailLine) {
        List<Map<String, String>> result = new ArrayList<>();

        Sheet sheet = workbook.getSheetAt(sheetIndex);

        // 获取第一行内容,作为标题内容
        Row titileRow = sheet.getRow(0);
        Map<String, String> titleContent = new LinkedHashMap<>();
        for (int i = 0; i < titileRow.getLastCellNum(); i++) {
            Cell cell = titileRow.getCell(i);
            titleContent.put(cell.getStringCellValue(), cell.getStringCellValue());
        }
        result.add(titleContent);

        // 获取正文内容
        Row row;
        for (Integer i = startLine; i < sheet.getLastRowNum() - tailLine + 1; i++) {
            row = sheet.getRow(i);
            Map<String, String> rowContent = new HashMap<>();

            for (Cell cell : row) {
                String returnStr;
                boolean isMergedCell  = this.isMergedCell(sheet, i, cell.getColumnIndex());

                if (isMergedCell) {
                    returnStr = this.getMergedRegionValue(sheet, row.getRowNum(), cell.getColumnIndex());
                } else {
                    returnStr = cell.getRichStringCellValue().getString();
                }

                rowContent.put(titileRow.getCell(cell.getColumnIndex()).getStringCellValue(), returnStr);
            }
            result.add(rowContent);
        }

        return result;
    }

    /**
     * 判断指定的单元格是否是合并单元格
     *
     * @param sheet Excel 指定的 Sheet 表
     * @param row 行下标
     * @param column 列下标
     * @return 是否为合并的单元格
     */
    private boolean isMergedCell(Sheet sheet, int row, int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if(row >= firstRow && row <= lastRow && (column >= firstColumn && column <= lastColumn)){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取合并单元格的值
     *
     * @param sheet 指定的值
     * @param row 行号
     * @param column 列好
     * @return 合并单元格的值
     */
    private String getMergedRegionValue(Sheet sheet, int row, int column){
        int sheetMergeCount = sheet.getNumMergedRegions();
        for(int i = 0 ; i < sheetMergeCount ; i++){
            CellRangeAddress ca = sheet.getMergedRegion(i);
            int firstColumn = ca.getFirstColumn();
            int lastColumn = ca.getLastColumn();
            int firstRow = ca.getFirstRow();
            int lastRow = ca.getLastRow();
            if(row >= firstRow && row <= lastRow && (column >= firstColumn && column <= lastColumn)) {
                Row fRow = sheet.getRow(firstRow);
                Cell fCell = fRow.getCell(firstColumn);
                return this.getCellValue(fCell) ;
            }
        }
        return null ;
    }

    /**
     * 获取单元格的值
     *
     * @param cell Cell 对象
     * @return 单元格的值
     */
    private String getCellValue(Cell cell){
        if(cell == null) {
            return "";
        }

        if(cell.getCellTypeEnum() == CellType.STRING){
            return cell.getStringCellValue();
        } else if(cell.getCellTypeEnum() == CellType.BOOLEAN){
            return String.valueOf(cell.getBooleanCellValue());
        } else if(cell.getCellTypeEnum() == CellType.FORMULA){
            return cell.getCellFormula() ;
        } else if(cell.getCellTypeEnum() == CellType.NUMERIC){
            return String.valueOf(cell.getNumericCellValue());
        }

        return "";
    }
}

下面是将生成的List<Map<String, String>> excel数据插入到excel表中的工具类

package com.cdcas;

import com.cdcas.mapper.DirectoryTreeMapper;
import com.cdcas.pojo.DirectoryTree;
import com.cdcas.utils.ExcelDataUtil;
import com.cdcas.utils.ExcelUtil;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;

import java.io.FileInputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author zhaozhixin
 * @Date 2023/8/7 15:01
 * @注释
 */   //983
@SpringBootTest
public class Test2 {

    /**
     * 替换和插入
     *
     * @param parentPath
     * @param directoryTree
     */
    private String Insert(String parentPath, DirectoryTree directoryTree, String name) {
        directoryTree.setName(name);
        directoryTree.setDescription(parentPath);
        directoryTreeMapper.insert(directoryTree);
        directoryTree.setPath(parentPath + "." + directoryTree.getId());
        directoryTreeMapper.updateDirectoryTree(directoryTree);
        return directoryTree.getId();
    }

    /**
     * 通过名称查询父路径
     *
     * @param name
     * @return
     */
    private String getParentPathByName(String name) {
        DirectoryTree parent = directoryTreeMapper.getOneByName(name);
        return parent.getPath();
    }

    @Test
    public void get() {
        String path = directoryTreeMapper.getPath(1);
        System.out.println(path);
    }

    @Autowired
    private ExcelUtil excelUtil;

    @Autowired
    private ExcelDataUtil excelDataUtil;
    @Autowired
    private DirectoryTreeMapper directoryTreeMapper;

    @Test
    public void insert() throws Exception {
        //读取一个excel
        List<Map<String, String>> maps = excelUtil.readExcelFile("C:\\Users\\20745\\Desktop\\git库\\gitee\\pg-demo-itree\\src\\main\\resources\\国土规划目录树.xlsx", 0, 1, 0);
        maps.remove(0);
        System.out.println(maps);
        for (Map<String, String> map : maps) {
            String A1 = map.get("A1");
            String A2 = map.get("A2");
            String A3 = map.get("A3");
            String A4 = map.get("A4");
            String A5 = map.get("A5");
            String A6 = map.get("A6");
            String A7 = map.get("A7");
            String A8 = map.get("A8");
            String A9 = map.get("A9");
            StringBuilder parentPath = new StringBuilder();//用来拼接父节点id
            parentPath.append("04e19944aa1d3d8bc13971b4488a4e0d");//这是根节点id
            if (A1 != null && !"".equals(A1)) {
                    //二级节点  根节点为root
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A1;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){

                    }else {
                        parentPath.append(".").append(id);
                    }
            }
            if (A2 != null && !"".equals(A2)) {
                    //拿到所有同名的行
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A2;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
            }
            if (A3 != null && !"".equals(A3)) {
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A3;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
            }
            if (A4 != null && !"".equals(A4)) {
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A4;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
            }
            if (A5 != null && !"".equals(A5)) {
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A5;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
            }
            if (A6 != null && !"".equals(A6)) {
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A6;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
            }
            if (A7 != null && !"".equals(A7)) {
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A7;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
            }
            if (A8 != null && !"".equals(A8)) {
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A8;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
                }
            if (A9 != null && !"".equals(A9)) {
                    DirectoryTree directoryTree = new DirectoryTree();
                    String name = A9;
                    String id = isExtis(name, directoryTree, parentPath.toString());
                    if (id==null){
                    }else {
                        parentPath.append(".").append(id);
                    }
            }
        }
    }

    /**
     * 判断一个同名的是否存在在其它数 返回当前节点id
     * @param name
     * @param directoryTree
     * @param parentPath
     */
    private String isExtis(String name,DirectoryTree directoryTree,String parentPath){
        //最终标识  开始表明不存在
        boolean isExtis = false;
        //获取到所有的和name同名的行
        List<DirectoryTree> oneByNames = directoryTreeMapper.getListByName(name);
        //如果没有同名的直接插入
        if (CollectionUtils.isEmpty(oneByNames)){
            directoryTree.setName(name);
            directoryTree.setDescription(parentPath);
            directoryTreeMapper.insert(directoryTree);
            directoryTree.setPath(parentPath+"."+directoryTree.getId());
            directoryTreeMapper.updateDirectoryTree(directoryTree);
            return directoryTree.getId();
        }
        //如果有同名的,需要判断父路径是否相同
        String path = "";
        int lastIndexOf = 0;
        for (DirectoryTree oneByName : oneByNames) {
            if (oneByName!=null) {
                path = oneByName.getPath();
                lastIndexOf = path.lastIndexOf(".");
            }
            //重复的数据应该也要被插入进去  查出的父路径传入的父路径进行对比
            if (path.substring(0,lastIndexOf).equals(parentPath)){
                isExtis = true;
            }
        }
        //最后如果同名的数据但是父路径不相同,就需要插入进去
        if (!isExtis){
                directoryTree.setName(name);
                directoryTree.setDescription(parentPath);
                directoryTreeMapper.insert(directoryTree);
                directoryTree.setPath(parentPath+"."+directoryTree.getId());
                directoryTreeMapper.updateDirectoryTree(directoryTree);
            return directoryTree.getId();
        }
        return oneByNames.get(oneByNames.size()-1).getId();
    }
    //查看重复条数和 总条数
    @Test
    public void look() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\20745\\Desktop\\git库\\gitee\\pg-demo-itree\\src\\main\\resources\\国土规划目录树.xlsx");
        List<Map<String, String>> maps = excelDataUtil.readExcel(fileInputStream);

        int count = 0;
        HashSet<String> set = new HashSet();
        for (Map<String, String> map : maps) {
            if (StringUtils.isNotBlank(map.get("A1"))) {
                set.add(map.get("A1"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A2"))) {
                set.add(map.get("A2"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A3"))) {
                set.add(map.get("A3"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A4"))) {
                set.add(map.get("A4"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A5"))) {
                set.add(map.get("A5"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A6"))) {
                set.add(map.get("A6"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A7"))) {
                set.add(map.get("A7"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A8"))) {
                set.add(map.get("A8"));
                count++;
            }
            if (StringUtils.isNotBlank(map.get("A9"))) {
                set.add(map.get("A9"));
                count++;
            }
        }
        System.out.println(count);
        System.out.println(set.size());
    }
}

最后插入的数据大概是这样[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-449koFvW-1691660171266)(C:\Users\20745\AppData\Roaming\Typora\typora-user-images\image-20230810172712617.png)]

注意这里的path!!!!!是id拼起来的具有目录层次的!![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UHQCBxy4-1691660171267)(C:\Users\20745\AppData\Roaming\Typora\typora-user-images\image-20230810173241809.png)]

这些关于目录树的基本操作,楼主写了一个小demo放在gitee上面了。本人不会算法,里面写的很菜见谅哈哈。喜欢的点个赞谢谢<.>! gitee地址:pg-demo-itree: 基于postgresql的Itree功能实现目录树的操作 (gitee.com)


http://www.niftyadmin.cn/n/4948633.html

相关文章

CS1988|C#无法在异步方法中使用ref,in,out类型的参数的问题

CS1988|C#无法在异步方法中使用ref,in,out类型的参数 &#x1f300;|场景&#xff1a; BlazorServer的场景中推荐使用异步方法&#xff0c;使用ref,out,in为参数前缀则报错CS1988 原因如下: ref parameters are not supported in async methods because the method may not h…

linux系统中的中文显示问题

经常遇到这种情况&#xff1a;某些项目的文件中不可避免地包含有中文&#xff0c;在Windows系统中没有任何问题&#xff0c;拷到Linux系统中就出问题了。 1. Linux系统设置 $echo $LANG en_US.iso885915 朋友建议我设置为&#xff1a; export LANGzh_CN.utf8 但我这样设置之…

vue3中使用mock.js

什么是mockjs Mock.js 是一个用于生成随机数据的模拟数据生成库。它可以帮助开发人员在前端开发中创建模拟接口&#xff0c;以便进行测试和开发。 以下是 Mock.js 的一些主要功能和用法&#xff1a; 生成随机数据&#xff1a;Mock.js 提供了丰富的数据模板语法&#xff0c;可以…

x86 Linux系统下构建交叉编译工具(2)

接前一篇文章&#xff1a;x86 Linux系统下构建交叉编译工具&#xff08;1&#xff09; 本文参考以下文章&#xff1a; 建立交叉编译器 for arm &#xff08;binutils-2.17 gcc-3.4.6 glibc-2.3.6&#xff09; 特此致谢&#xff01; 上一回准备好了编译构建的环境&#xff0c;…

使用Druid解析SQL,获取SQL中所有使用的表

一、sqlParse组成 Druid SQL Parser分三个模块&#xff1a; - Parser - AST - Visitor 1.1 Parser parser是将输入文本转换为ast&#xff08;抽象语法树&#xff09;&#xff0c;parser有包括两个部分&#xff0c;Parser和Lexer&#xff0c;其中Lexer实现词法分析&#x…

request发送http请求

今天正式开始为大家介绍接口自动化&#xff0c;相信很多做测试的朋友&#xff0c;都用过一些工具&#xff0c;比如jmeter&#xff0c;loadrunner&#xff0c;postman等等&#xff0c;所以今天先给那些基础不太好的同学&#xff0c;先讲讲postman如何来测接口以及如何用pthon代码…

无涯教程-Perl - tell函数

描述 此函数返回指定FILEHANDLE中读取指针的当前位置(以字节为单位)。如果省略FILEHANDLE,则它将返回上次访问的文件中的位置。 语法 以下是此函数的简单语法- tell FILEHANDLEtell返回值 此函数以字节为单位返回当前文件位置。 例 以下是显示其基本用法的示例代码,要检…

接口测试重点方面

主要包括以下几个方面&#xff1a; 1.检查接口的功能&#xff1a;检查接口的功能有没有实现&#xff0c;也就是请求会不会成功&#xff0c;如果不成功会不会返回错误代号&#xff08;或错误信息&#xff09;&#xff1b; 2.检查接口返回的数据&#xff1a;检查接口返回的数据、…