这篇文章主要介绍了SpringBoot返回给前端一个tree结构的实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
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;
    }
}到此这篇关于Springboot实现给前端返回一个tree结构方法的文章就介绍到这了,更多相关Springboot tree结构内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Springboot实现给前端返回一个tree结构方法
				
        
 
            
        - SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
 - Java实现顺序表的操作详解 2023-05-19
 - Java中的日期时间处理及格式化处理 2023-04-18
 - Spring Security权限想要细化到按钮实现示例 2023-03-07
 - 深入了解Spring的事务传播机制 2023-06-02
 - Springboot整合minio实现文件服务的教程详解 2022-12-03
 - JSP页面间传值问题实例简析 2023-08-03
 - JSP 制作验证码的实例详解 2023-07-30
 - 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
 - ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
 
						
						
						
						
						
				
				
				
				