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

网友投稿 730 2022-10-03

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

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

目录基于springboot+vue的测试平台开发一、引用 HutoolUtil二、建表三、后端接口实现1. Controller 层2. DAO层3. Service 层四、测试一下1. 测试结构数据2. 测试新增默认

基于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。

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

cn.hutool

hutool-all

5.7.12

官方文档:https://hutool-/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 层

新建 Apihttp://ModuleController 类,添加一个处理器方法 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 {

}

3. Service 层

实现 getNodeTreeByProjectId 方法。

public List> getNodeTreeByProjectId(Long projectId) {

this.getDefaultNode(projectId);

// 根据 projectId 查询所有节点

QueryWrapper wrapperApiModule = new QueryWrapper<>();

List 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> 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 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>类型,可以直接塞到统一结果里去返回。

四、测试一下

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",

"pohttp://s": 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处理树形结构的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ICE 原理学习
下一篇:h264 丢包花屏处理的一个想法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~