JAVA如何把資料庫的資料處理成樹形結構
- 💨前言
- 😎實作思路😎
- 🧡完整代碼🧡
- 😜總結-核心代碼😜
💨前言

不知道大家在做專案的時候有沒有接觸到將平平無奇資料結合處理成有層次的資料呢,類似下面這樣

或者 生活處處都有,我想大家都應該接觸過的,下面直接看怎么實作,我會大概講一下思路,當然也可以直接跳到最后去看代碼實作的哈
follow me!go go go!
?此篇文章也只是一個簡單的學習記錄,不詳細的對代碼進行講解
😎實作思路😎
首先一般資料庫的模型設計如下

sql腳本
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uuid` varchar(64) NOT NULL,
`name` varchar(100) NOT NULL COMMENT '名稱',
`sort` int(11) DEFAULT NULL COMMENT '排序',
`parent_uuid` varchar(64) NOT NULL DEFAULT '-1' COMMENT '父親 無父級為-1',
`level` varchar(10) NOT NULL COMMENT '產品層級',
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COMMENT='產品表';
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES ('1', '4dbf40d2-2af7-425c-a103-0349caaa26cf', '生產類', '1', '-1', '1', '2021-09-23 15:34:36');
INSERT INTO `product` VALUES ('2', '3062deff-8ec7-44c4-bd4e-88fe3c7b835c', '22', '1', '4dbf40d2-2af7-425c-a103-0349caaa26cf', '2', '2021-09-23 15:37:20');
INSERT INTO `product` VALUES ('3', '32afe426-9337-41c1-83e8-caf3248ba57e', '互聯網資訊', '2', '4dbf40d2-2af7-425c-a103-0349caaa26cf', '2', '2021-09-23 15:38:19');
INSERT INTO `product` VALUES ('4', '34c5239f-db2d-4394-b367-a57f8ae6f8ff', '33', '1', '3062deff-8ec7-44c4-bd4e-88fe3c7b835c', '3', '2021-09-23 15:53:29');
INSERT INTO `product` VALUES ('5', '19eedcd3-aa7f-4a2d-8182-d3f795e99b9d', '44', '1', '34c5239f-db2d-4394-b367-a57f8ae6f8ff', '4', '2021-09-23 15:53:56');
我們觀察一下,可以發現我們的關注重點在name、uuid、parent_uuid上面:
name:分類名稱
uuid:UUID 是 通用唯一識別碼(Universally Unique Identifier)的縮寫,是一種軟體建構的標準,其目的,是讓分布式系統中的所有元素,都能有唯一的辨識資訊,而不需要通過中央控制端來做辨識資訊的指定,這里可以簡單看作一個唯一標識碼(類似于ID但不等于ID)
parent_uuid:子類的父類UUID,最高級規定為-1(這個可以自己定義,不會有相同的就好)
下面就是我創建的模擬資料

想要實作數形狀結構,肯定要以某一屬性來作為突破口,它就是parent_uuid,那么到底是如何實作的 來看具體代碼
🧡完整代碼🧡
只貼重點代碼
首先使用了Mabatis-generator生成了通用后端代碼,結構如下:

ProductController.class
package com.csdn.caicai.test.modules.product.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.csdn.caicai.test.modules.product.dto.ProductRsp;
import com.csdn.caicai.test.modules.product.biz.IProductBiz;
import java.util.List;
/**
* 產品表
*
* @author
* @date
*/
@RestController
@Api(tags = {"產品表"})
@RequestMapping("/caicai/product")
@Validated
public class ProductController {
private static final Logger log = LoggerFactory.getLogger(ProductController.class);
@Autowired
private IProductBiz productBiz;
/**
* 產品樹
*/
@ApiOperation(value = "產品樹")
@RequestMapping(path = "/tree", method = RequestMethod.GET)
public List<ProductRsp> tree() {
return productBiz.tree();
}
}
IProductBiz.class
package com.csdn.caicai.test.modules.product.biz;
import com.csdn.caicai.test.modules.product.dto.ProductRsp;
import java.util.List;
/**
* @author
* @date
*/
public interface IProductBiz {
List<ProductRsp> tree();
}
ProductBiz.class
package com.csdn.caicai.test.modules.product.biz;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;
import tk.mybatis.mapper.entity.Example;
import com.csdn.caicai.test.modules.product.service.IProductService;
import com.csdn.caicai.test.modules.product.dao.entity.ProductEntity;
import com.csdn.caicai.test.modules.product.dto.ProductReq;
import com.csdn.caicai.test.modules.product.dto.ProductRsp;
import static java.util.stream.Collectors.toList;
/**
* @author
* @date
*/
@Service("productBiz")
public class ProductBiz implements IProductBiz {
@Autowired
private IProductService productService;
/**
* 根據條件查詢
*
* @param productReq
* @return
*/
public List<ProductEntity> selectByCondition(ProductReq productReq) {
Example example = new Example(ProductEntity.class);
//下面添加自定義收索條件
return productService.selectByExample(example);
}
@Override
public List<ProductRsp> tree() {
ProductReq req = new ProductReq();
List<ProductRsp> list = selectByCondition(req).stream().map(this::productConvert).collect(Collectors.toList());
return buildTree(list, req.getParentUuid());
}
private ProductRsp productConvert(ProductEntity e) {
ProductRsp orgNode = new ProductRsp();
orgNode.setId(e.getId());
orgNode.setUuid(e.getUuid());
orgNode.setName(e.getName());
orgNode.setLevel(e.getLevel());
orgNode.setSort(e.getSort());
orgNode.setParentUuid(e.getParentUuid());
return orgNode;
}
public static List<ProductRsp> buildTree(List<ProductRsp> all, String parentUuid) {
if (CollectionUtils.isEmpty(all))
return Lists.newArrayList();
List<ProductRsp> parentList = all.stream()
.filter(e -> StringUtils.isBlank(e.getParentUuid())
|| "-1".equals(e.getParentUuid())
|| e.getParentUuid().equals(parentUuid))
.collect(toList());
getSubList(parentList, all);
return parentList;
}
private static void getSubList(List<ProductRsp> parentList, List<ProductRsp> all) {
parentList.forEach(e -> {
List<ProductRsp> subList = all.stream().filter(o -> o.getParentUuid().equals(e.getUuid())).collect(toList());
e.setSubList(subList);
if (!CollectionUtils.isEmpty(subList))
getSubList(subList, all);
});
}
}
ProductReq.class
package com.csdn.caicai.test.modules.product.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @author
* @date
*/
@ApiModel(value = "ProductReq", description = "產品表")
@Data
public class ProductReq implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@ApiModelProperty(value = "", name = "id")
private Long id;
/**
*
*/
@ApiModelProperty(value = "", name = "uuid")
private String uuid;
/**
* 名稱
*/
@ApiModelProperty(value = "名稱", name = "name")
private String name;
/**
* 排序
*/
@ApiModelProperty(value = "排序", name = "sort")
private Integer sort;
/**
* 父親 無父級為-1
*/
@ApiModelProperty(value = "父親 無父級為-1", name = "parentUuid")
private String parentUuid;
/**
* 產品層級
*/
@ApiModelProperty(value = "產品層級", name = "level")
private String level;
}
ProductRsp.class
package com.csdn.caicai.test.modules.product.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* @author
* @date
*/
@ApiModel(value = "ProductRsp", description = "產品表")
@Data
public class ProductRsp implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@ApiModelProperty(value = "", name = "id")
private Long id;
/**
*
*/
@ApiModelProperty(value = "", name = "uuid")
private String uuid;
/**
* 名稱
*/
@ApiModelProperty(value = "名稱", name = "name")
private String name;
/**
* 排序
*/
@ApiModelProperty(value = "排序", name = "sort")
private Integer sort;
/**
* 父親 無父級為-1
*/
@ApiModelProperty(value = "父親 無父級為-1", name = "parentUuid")
private String parentUuid;
/**
* 產品層級
*/
@ApiModelProperty(value = "產品層級", name = "level")
private String level;
/**
*
*/
@ApiModelProperty(value = "", name = "createTime")
private Date createTime;
@ApiModelProperty(value = "下屬產品", name = "subList")
private List<ProductRsp> subList;
}
測驗一下

可以看到,實作了我們的效果
😜總結-核心代碼😜
上面羅里吧嗦,其實核心代碼就是以下代碼,親們來試著理解一下,然后就可以在此基礎上美化一下就好了:
ProductRsp、ProductReq 是物體類,可以自行替換里面的內容
private ProductRsp productConvert(ProductEntity e) {
ProductRsp orgNode = new ProductRsp();
orgNode.setId(e.getId());
orgNode.setUuid(e.getUuid());
orgNode.setName(e.getName());
orgNode.setLevel(e.getLevel());
orgNode.setSort(e.getSort());
orgNode.setParentUuid(e.getParentUuid());
return orgNode;
}
public static List<ProductRsp> buildTree(List<ProductRsp> all, String parentUuid) {
if (CollectionUtils.isEmpty(all))
return Lists.newArrayList();
List<ProductRsp> parentList = all.stream()
.filter(e -> StringUtils.isBlank(e.getParentUuid())
|| "-1".equals(e.getParentUuid())
|| e.getParentUuid().equals(parentUuid))
.collect(toList());
getSubList(parentList, all);
return parentList;
}
private static void getSubList(List<ProductRsp> parentList, List<ProductRsp> all) {
parentList.forEach(e -> {
List<ProductRsp> subList = all.stream().filter(o -> o.getParentUuid().equals(e.getUuid())).collect(toList());
e.setSubList(subList);
if (!CollectionUtils.isEmpty(subList))
getSubList(subList, all);
});
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/302546.html
標籤:其他
上一篇:我Python采集了新榜熱門內容,原來這就是別人能成為自媒體大佬的秘密!
下一篇:單片機的最小系統及IO口結構
