Springboot实现给前端返回一个tree结构方法
作者:夜色架构师 时间:2022-04-02 23:37:07
1:首先我们看一下数据库的表:
这里的pid就是代表他的父节点id,如果没有父节点,那么pid就是0,上面的表就可以看作是一个tree结构,那么我们怎样去将这个tree结构返回给前端呢?
2:首先写好数据库对应的实体类和Dto层:
package com.wyr.modules.example.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.io.Serializable;
/**
* @author jianyijun
* @date 2022-07-02
*/
@Data
@TableName("store_category")
public class Category implements Serializable {
/** 商品分类表ID */
@TableId
private Integer id;
/** 父id */
@NotNull
private Integer pid;
/** 分类名称 */
@NotBlank
private String cateName;
/** 排序 */
private Integer sort;
/** 图标 */
private String pic;
/** 是否推荐 */
private Integer isShow;
/** 添加时间 */
@TableField(fill= FieldFill.INSERT)
private Timestamp createTime;
/** 更新时间 */
@TableField(fill= FieldFill.INSERT_UPDATE)
private Timestamp updateTime;
/** 删除状态 */
private Integer isDel;
public void copy(Category source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}
Dto层:
package com.wyr.modules.example.service.dto;
import lombok.Data;
import java.sql.Timestamp;
import java.io.Serializable;
import java.util.List;
/**
* @author jianyijun
* @date 2022-07-02
*/
@Data
public class CategoryDto implements Serializable {
/** 商品分类表ID */
private Long id;
/** 父id */
private Long pid;
/** 分类名称 */
private String cateName;
/** 排序 */
private Integer sort;
/** 图标 */
private String pic;
/** 是否推荐 */
private Integer isShow;
/** 添加时间 */
private Timestamp createTime;
/** 更新时间 */
private Timestamp updateTime;
/** 删除状态 */
private Integer isDel;
private List<CategoryDto> children;
}
这里注意一下Dto层多余的字段:private List<CategoryDto> children;
,这个也就是一个自己的集合,代表自己的孩子
3:这里介绍一下什么是Dto层,以及一些区别:
(1) entity 里的每一个字段,与数据库相对应,
(2) vo 里的每一个字段,是和你前台 html 页面相对应,
(3) dto 这是用来转换从 entity 到 vo,或者从 vo 到 entity 的中间的东西 。(DTO中拥有的字段应该是entity中或者是vo中的一个子集)
4:然后是controller层:
ResponseEntity<Object>
不用管,是一个通用的返回数据封装类,然后中间那行就是最里面使用了QueryHelp工具,可以不写SQL语句进行条件查询,然后convert就是一个复制方法,可以类似于BeanUtils里面的copy等等,这就是先将查询到的list复制给Dto类,然后我们进入接下来的Service方法:buildTree:
5:业务层:
/**
* 构建分类树
* @param categoryDtos 原始数据
* @return
*/
@Override
public Map<String, Object> buildTree(List<CategoryDto> categoryDtos) {
List<CategoryDto> trees = new ArrayList<>();
Set<Long> ids = new HashSet<>();
for (CategoryDto categoryDto :categoryDtos) {
if (categoryDto.getPid() == 0) {
trees.add(categoryDto);
}
for (CategoryDto it : categoryDtos) {
if (it.getPid().equals(categoryDto.getId())) {
if (categoryDto.getChildren() == null) {
categoryDto.setChildren(new ArrayList<>());
}
categoryDto.getChildren().add(it);
ids.add(it.getId());
}
}
}
Map<String, Object> map = new HashMap<>(2);
if (trees.size() == 0){
trees = categoryDtos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
}
map.put("content",trees);
map.put("totalElements", categoryDtos.size());
return map;
}
}
来源:https://blog.csdn.net/justleavel/article/details/125584029
标签:Springboot,前端,tree
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SSM框架+Plupload实现分块上传大文件示例
2023-06-01 01:58:21
![](https://img.aspxhome.com/file/2023/8/63048_0s.png)
使用注解@Validated效验VO参数是否合规
2023-10-27 20:13:01
浅析java中的取整(/)和求余(%)
2023-04-30 23:46:23
HttpServletResponse乱码问题_动力节点Java学院整理
2021-10-18 17:48:51
![](https://img.aspxhome.com/file/2023/2/78902_0s.jpg)
java springmvc实现验证码功能
2022-09-07 12:12:41
![](https://img.aspxhome.com/file/2023/1/61781_0s.jpg)
java中使用数组进行模拟加密的方法
2023-11-18 15:37:39
实例讲解JAVA设计模式之备忘录模式
2023-08-29 16:31:19
C#实现将记事本中的代码编译成可执行文件的方法
2022-03-05 02:17:53
![](https://img.aspxhome.com/file/2023/8/78118_0s.png)
java8新特性之日期时间API
2023-06-20 09:15:50
![](https://img.aspxhome.com/file/2023/2/57602_0s.png)
Spring+Vue整合UEditor富文本实现图片附件上传的方法
2022-09-27 10:36:13
![](https://img.aspxhome.com/file/2023/3/78033_0s.png)
Java超详细分析@Autowired原理
2023-11-25 05:37:44
![](https://img.aspxhome.com/file/2023/9/60059_0s.png)
mybatis-plus分页查询的实现示例
2023-11-25 04:57:57
![](https://img.aspxhome.com/file/2023/5/60025_0s.png)
Java实现图形界面计算器
2023-06-05 11:53:23
![](https://img.aspxhome.com/file/2023/7/65757_0s.jpg)
Java在Excel中创建透视表方法解析
2021-11-03 05:49:45
![](https://img.aspxhome.com/file/2023/5/62955_0s.png)
java实现波雷费密码算法示例代码
2022-09-14 23:30:28
![](https://img.aspxhome.com/file/2023/3/62073_0s.png)
Java SE求解汉诺塔问题的示例代码
2022-05-10 23:44:30
![](https://img.aspxhome.com/file/2023/1/61291_0s.png)
手把手教你设置IntelliJ IDEA 的彩色代码主题的图文教程
2022-08-22 00:15:25
![](https://img.aspxhome.com/file/2023/0/74980_0s.jpg)
Flutter之Timer实现短信验证码获取60s倒计时功能的代码
2023-07-20 20:00:30
![](https://img.aspxhome.com/file/2023/8/89528_0s.gif)
Java命名规则详细总结
2023-11-14 12:20:55
Java之InputStreamReader类的实现
2023-09-03 01:28:17