广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot vue接口测试HutoolUtil TreeUtil处理树形结构
  • 295
分享到

springboot vue接口测试HutoolUtil TreeUtil处理树形结构

2024-04-02 19:04:59 295人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录基于SpringBoot+Vue的测试平台开发一、引用 HutoolUtil二、建表三、后端接口实现1. Controller 层2. DAO层3. Service 层四、测试一

基于springboot+vue的测试平台开发

继续更新

上次完成了接口定义功能的前端页面,那么后端现在开始逐一实现对应的功能,首先就是提供模块列表接口,这个模块是支持子层级的,所以大概结构是这样:

[
  {
    id: 1,
    label: '默认',
    children: [
      {
        id: 4,
        label: '二级子模块1',
        children: [
          {
            id: 9,
            label: '三级子模块1'
          }, 
          {
            id: 10,
            label: '三级子模块2'
          }
        ]
      }
    ]
  }, 
  {
    id: 2,
    label: '一级子模块2',
    children: [
      {
        id: 5,
        label: '二级子模块 1'
      }, 
      {
        id: 6,
        label: '二级子模块 2'
      }
    ]
  }
]

通常来说,可以写递归代码来找出子层级的数据,然后再进行封装返回出来,比较麻烦。

后来发现 HutoolUtil 中有个工具类 TreeUtil 可以完成我需求,非常便捷,本次就使用它来实现。

下面来完成接口功能的开发。

一、引用 HutoolUtil

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关api学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

Hutool中的工具方法来自每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;

Hutool是项目中“util”包友好的替代,它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。

要使用它直接添加依赖即可:

<!--hutool-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.7.12</version>
    </dependency>

官方文档:https://www.hutool.cn/docs/#/

内容很详细,不仅后面的树结构工具,像常用的集合类、JSON日志缓存、文件、线程并发等等应有尽有。

二、建表

给模块建一张新表api_module:

CREATE TABLE `api_module` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
  `projectId` bigint NOT NULL COMMENT '该节点所属项目id',
  `name` varchar(64) COLLATE utf8mb4_general_ci NOT NULL COMMENT '节点名称',
  `parentId` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '父节点id',
  `level` int DEFAULT '1' COMMENT '节点层级',
  `createTime` datetime NOT NULL DEFAULT '1900-01-01 00:00:00' COMMENT '创建时间',
  `updateTime` datetime NOT NULL DEFAULT '1900-01-01 00:00:00' COMMENT '更新时间',
  `pos` double DEFAULT NULL COMMENT '节点顺序位置',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='模块表';

重要字段:

projectId:与项目进行关联

parentId:该节点的父节点,一级目录的父节点我会设置为 0 。

level:该节点对应层级,从 1 开始。

pos:表示该节点在父节点下的位置顺序。

三、后端接口实现

1. Controller 层

新建 ApiModuleController 类,添加一个处理器方法 getnodeByProjectId,通过项目 ID 查询出下面的所有模块。

package com.pingguo.bloomtest.controller;
import com.pingguo.bloomtest.common.Result;
import com.pingguo.bloomtest.service.ApiModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.WEB.bind.annotation.*;
@RestController
@RequestMapping("module")
public class ApiModuleController {
    @Autowired
    ApiModuleService apiModuleService;
    @GetMapping("/list/{projectId}")
    public Result getNodeByProjectId(@PathVariable Long projectId) {
        return Result.success(apiModuleService.getNodeTreeByProjectId(projectId));
    }
}

2. DAO层

dao 层自然也要有。

package com.pingguo.bloomtest.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pingguo.bloomtest.pojo.ApiModule;
import org.springframework.stereotype.Repository;
@Repository
public interface ApiModuleDAO extends BaseMapper<ApiModule> {
}

3. Service 层

实现 getNodeTreeByProjectId 方法。

public List<Tree<String>> getNodeTreeByProjectId(Long projectId) {
        this.getDefaultNode(projectId);
        // 根据 projectId 查询所有节点
        QueryWrapper<ApiModule> wrapperApiModule = new QueryWrapper<>();
        List<ApiModule> apiModules = apiModuleDAO.selectList(wrapperApiModule.eq("projectId", projectId));
        // 配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 ,即返回列表里对象的字段名
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setWeighTKEy("pos");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setChildrenKey("children");
        // 最大递归深度
//        treeNodeConfig.setDeep(5);
        treeNodeConfig.setNameKey("name");
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(apiModules, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setWeight(treeNode.getPos());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("projectId", treeNode.getProjectId());
                    tree.putExtra("level", treeNode.getLevel());
                    tree.putExtra("label", treeNode.getName());
                    tree.putExtra("createTime", treeNode.getCreateTime());
                    tree.putExtra("updateTime", treeNode.getUpdateTime());
                });
        return treeNodes;
    }

这里开头有个方法 getDefaultNode,在这里面会判断当前项目下是否有默认模块,没有则添加默认模块。

private void getDefaultNode(Long projectId) {
        QueryWrapper<ApiModule> wrapperApiModule = new QueryWrapper<>();
        wrapperApiModule.eq("projectId", projectId)
                        .eq("pos", 1.0);
        // 判断当前项目下是否有默认模块,没有则添加默认模块
        if (apiModuleDAO.selectCount(wrapperApiModule) == 0) {
            ApiModule apiModule = new ApiModule();
            apiModule.setName("默认");
            apiModule.setPos(1.0);
            apiModule.setLevel(1);
            apiModule.setParentId(0L);
            apiModule.setCreateTime(new Date());
            apiModule.setUpdateTime(new Date());
            apiModule.setProjectId(projectId);
            apiModuleDAO.insert(apiModule);
        }
    }

然后通过 项目id 把项目下所有的数据查询出来:

接下来使用 TreeUtil 来完成树结构处理。

首先,创建一个配置类 TreeNodeConfig 对象,在这个对象里设置属性,对应的就是返回出来的字段名。

还可以设置最大递归深度,也可以不设。我测试之后就注释掉了,先不加限制。

最后就是构建树结构 treeNodes,完成处理后返回给 controller 层。

因为我要返回的还有其他的字段,可以使用tree.putExtra来添加要返回的其他字段,比如:

tree.putExtra("projectId", treeNode.getProjectId());

第一个参数是定义的字段名称,第二个参数就是使用这个结点的 get 方法获取对应的属性值。

最后返回到上层的是List<Tree<String>>类型,可以直接塞到统一结果里去返回。

四、测试一下

1. 测试结构数据

测试一下接口,先手动网表里插入了对应结构的数据。

请求接口,传入 projectId 为 3。

{
    "code": 20000,
    "message": "成功",
    "data": [
        {
            "id": "9",
            "parentId": "0",
            "pos": 1.0,
            "name": "默认",
            "projectId": 3,
            "level": 1,
            "label": "默认",
            "createTime": "2021-09-29 10:50:00",
            "updateTime": "2021-09-29 10:50:00",
            "children": [
                {
                    "id": "14",
                    "parentId": "9",
                    "pos": 1.0,
                    "name": "默认-2",
                    "projectId": 3,
                    "level": 2,
                    "label": "默认-2",
                    "createTime": "1900-01-01 08:00:00",
                    "updateTime": "1900-01-01 08:00:00"
                },
                {
                    "id": "10",
                    "parentId": "9",
                    "pos": 1.0,
                    "name": "默认-1",
                    "projectId": 3,
                    "level": 2,
                    "label": "默认-1",
                    "createTime": "2021-10-01 08:00:00",
                    "updateTime": "1900-01-01 08:00:00",
                    "children": [
                        {
                            "id": "11",
                            "parentId": "10",
                            "pos": 1.0,
                            "name": "默认-1-1",
                            "projectId": 3,
                            "level": 3,
                            "label": "默认-1-1",
                            "createTime": "1900-01-01 08:00:00",
                            "updateTime": "1900-01-01 08:00:00",
                            "children": [
                                {
                                    "id": "12",
                                    "parentId": "11",
                                    "pos": 1.0,
                                    "name": "默认-1-1-1",
                                    "projectId": 3,
                                    "level": 4,
                                    "label": "默认-1-1-1",
                                    "createTime": "1900-01-01 08:00:00",
                                    "updateTime": "1900-01-01 08:00:00",
                                    "children": [
                                        {
                                            "id": "13",
                                            "parentId": "12",
                                            "pos": 1.0,
                                            "name": "默认-1-1-1-1",
                                            "projectId": 3,
                                            "level": 5,
                                            "label": "默认-1-1-1-1",
                                            "createTime": "1900-01-01 08:00:00",
                                            "updateTime": "1900-01-01 08:00:00"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

结果正确。

2. 测试新增默认

传入一个 projectId 为 4 ,localhost:8080/bloomtest/module/list/4:

{
    "code": 20000,
    "message": "成功",
    "data": [
        {
            "id": "15",
            "parentId": "0",
            "pos": 1.0,
            "name": "默认",
            "projectId": 4,
            "level": 1,
            "label": "默认",
            "createTime": "2021-10-01 12:25:54",
            "updateTime": "2021-10-01 12:25:54"
        }
    ]
}

返回正确。

落库正常。

以上就是springboot vue接口测试HutoolUtil TreeUtil处理树形结构的详细内容,更多关于HutoolUtil TreeUtil处理树形结构的资料请关注编程网其它相关文章!

--结束END--

本文标题: springboot vue接口测试HutoolUtil TreeUtil处理树形结构

本文链接: https://www.lsjlt.com/news/149894.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作