广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot实现给前端返回一个tree结构方法
  • 741
分享到

Springboot实现给前端返回一个tree结构方法

2024-04-02 19:04:59 741人浏览 独家记忆

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

摘要

1:首先我们看一下数据库的表: 这里的pid就是代表他的父节点id,如果没有父节点,那么pid就是0,上面的表就可以看作是一个tree结构,那么我们怎样去将这个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;

@Data
@TableName("store_cateGory")
public class Category implements Serializable {
    
    @TableId
    private Integer 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;

@Data
public class CategoryDto implements Serializable {
    
    private Long 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:业务层:

 
    @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结构内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Springboot实现给前端返回一个tree结构方法

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

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

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

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

下载Word文档
猜你喜欢
  • Springboot实现给前端返回一个tree结构方法
    1:首先我们看一下数据库的表: 这里的pid就是代表他的父节点id,如果没有父节点,那么pid就是0,上面的表就可以看作是一个tree结构,那么我们怎样去将这个tree结构返回给前...
    99+
    2022-11-13
  • SpringBoot如何实现统一封装返回前端结果集
    这篇文章主要介绍了SpringBoot如何实现统一封装返回前端结果集的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot如何实现统一封装返回前端结果集文章都会有所收获,下面我们一起来看看吧。我们如...
    99+
    2023-07-02
  • SpringBoot实现统一封装返回前端结果集的示例代码
    在实际项目开发过程中、事实上我们经常用@restcontroller注释的方式,将相当于将返回数据的基本形式统一为JSON格式的数据。但是,由于我们的项目可能是由很多人开发的,所以我...
    99+
    2022-11-13
  • SpringBoot使用thymeleaf实现一个前端表格方法详解
    目录1. User 实体类2. Controller 类3. html 文件1. User 实体类 注:这里使用了 Lombok 技术,通过 @Data 注释自动创建 get,set...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作