Springboot實現(xiàn)給前端返回一個tree結(jié)構(gòu)方法
1:首先我們看一下數(shù)據(jù)庫的表:

這里的pid就是代表他的父節(jié)點id,如果沒有父節(jié)點,那么pid就是0,上面的表就可以看作是一個tree結(jié)構(gòu),那么我們怎樣去將這個tree結(jié)構(gòu)返回給前端呢?
2:首先寫好數(shù)據(jù)庫對應(yīng)的實體類和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;
/** 刪除狀態(tài) */
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;
/** 刪除狀態(tài) */
private Integer isDel;
private List<CategoryDto> children;
}這里注意一下Dto層多余的字段:private List<CategoryDto> children;,這個也就是一個自己的集合,代表自己的孩子
3:這里介紹一下什么是Dto層,以及一些區(qū)別:
(1) entity 里的每一個字段,與數(shù)據(jù)庫相對應(yīng),
(2) vo 里的每一個字段,是和你前臺 html 頁面相對應(yīng),
(3) dto 這是用來轉(zhuǎn)換從 entity 到 vo,或者從 vo 到 entity 的中間的東西 。(DTO中擁有的字段應(yīng)該是entity中或者是vo中的一個子集)
4:然后是controller層:

ResponseEntity<Object>不用管,是一個通用的返回數(shù)據(jù)封裝類,然后中間那行就是最里面使用了QueryHelp工具,可以不寫SQL語句進行條件查詢,然后convert就是一個復(fù)制方法,可以類似于BeanUtils里面的copy等等,這就是先將查詢到的list復(fù)制給Dto類,然后我們進入接下來的Service方法:buildTree:
5:業(yè)務(wù)層:

/**
* 構(gòu)建分類樹
* @param categoryDtos 原始數(shù)據(jù)
* @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;
}
}到此這篇關(guān)于Springboot實現(xiàn)給前端返回一個tree結(jié)構(gòu)方法的文章就介紹到這了,更多相關(guān)Springboot tree結(jié)構(gòu)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Debian 7 和 Debian 8 用戶安裝 Java 8的方法
Oracle Java 8 穩(wěn)定版本近期已發(fā)布,有很多新的特征變化。其中,有功能的程序支持通過“Lambda項目 ”,收到了一些安全更新和界面改進上的bug修復(fù),使得開發(fā)人員的工作更容易。2014-03-03
java接口類中的@selectProvider接口的使用及說明
這篇文章主要介紹了java接口類中的@selectProvider接口的使用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Java8函數(shù)式接口java.util.function速查大全
因為Java8引入了函數(shù)式接口,在java.util.function包含了幾大類函數(shù)式接口聲明,這篇文章主要給大家介紹了關(guān)于Java8函數(shù)式接口java.util.function速查的相關(guān)資料,需要的朋友可以參考下2021-08-08
Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實例
這篇文章主要介紹了Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
JPA @Query時,無法使用limit函數(shù)的問題及解決
這篇文章主要介紹了JPA @Query時,無法使用limit函數(shù)的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot2底層注解@Configuration配置類詳解
這篇文章主要為大家介紹了SpringBoot2底層注解@Configuration配置類詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05

