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)文章
Java基礎(chǔ)之詳解基本數(shù)據(jù)類型的使用
今天給大家?guī)淼氖顷P(guān)于Java基礎(chǔ)的相關(guān)知識,文章圍繞著基本數(shù)據(jù)類型的使用展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06springboot項目整合druid數(shù)據(jù)庫連接池的實現(xiàn)
這篇文章主要介紹了springboot項目整合druid數(shù)據(jù)庫連接池的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04一篇文章帶你了解jdk1.8新特性--為什么使用lambda表達式
Lambda是一個匿名函數(shù),我們可以把Lambda表達式理解為是一段可以傳遞的代碼,本篇文章就帶你了解,希望能給你帶來幫助2021-08-08Java發(fā)送郵件javax.mail的實現(xiàn)方法
這篇文章主要為大家介紹了Java發(fā)送郵件javax.mail的實現(xiàn)方法,具有一定的參考價值,代碼都有詳細的注釋,感興趣的小伙伴們可以參考一下2016-01-01Spring Boot 如何將 Word 轉(zhuǎn)換為 PDF
這篇文章主要介紹了Spring Boot將Word轉(zhuǎn)換為 PDF,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08