最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA如何把數(shù)據(jù)庫的數(shù)據(jù)處理成樹形結(jié)構(gòu)

 更新時(shí)間:2021年09月24日 09:06:34   作者:菜菜bu菜  
本文介紹了JAVA如何把數(shù)據(jù)庫的數(shù)據(jù)處理成樹形結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

不知道大家在做項(xiàng)目的時(shí)候有沒有接觸到將平平無奇數(shù)據(jù)結(jié)合處理成有層次的數(shù)據(jù)呢,類似下面這樣

在這里插入圖片描述

或者 生活處處都有,我想大家都應(yīng)該接觸過的,下面直接看怎么實(shí)現(xiàn),我會(huì)大概講一下思路,當(dāng)然也可以直接跳到最后去看代碼實(shí)現(xiàn)的哈

follow me!go go go!

❗此篇文章也只是一個(gè)簡單的學(xué)習(xí)記錄,不詳細(xì)的對(duì)代碼進(jìn)行講解

😎實(shí)現(xiàn)思路😎

首先一般數(shù)據(jù)庫的模型設(shè)計(jì)如下

在這里插入圖片描述

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 '父親 無父級(jí)為-1',
  `level` varchar(10) NOT NULL COMMENT '產(chǎn)品層級(jí)',
  `create_time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COMMENT='產(chǎn)品表';

-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES ('1', '4dbf40d2-2af7-425c-a103-0349caaa26cf', '生產(chǎn)類', '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', '互聯(lián)網(wǎng)信息', '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');

我們觀察一下,可以發(fā)現(xiàn)我們的關(guān)注重點(diǎn)在name、uuid、parent_uuid上面:

  • name:分類名稱
  • uuid:UUID 是 通用唯一識(shí)別碼(Universally Unique Identifier)的縮寫,是一種軟件建構(gòu)的標(biāo)準(zhǔn),其目的,是讓分布式系統(tǒng)中的所有元素,都能有唯一的辨識(shí)信息,而不需要通過中央控制端來做辨識(shí)信息的指定。這里可以簡單看作一個(gè)唯一標(biāo)識(shí)碼(類似于ID但不等于ID)
  • parent_uuid:子類的父類UUID,最高級(jí)規(guī)定為-1(這個(gè)可以自己定義,不會(huì)有相同的就好)

下面就是我創(chuàng)建的模擬數(shù)據(jù)

在這里插入圖片描述

想要實(shí)現(xiàn)數(shù)形狀結(jié)構(gòu),肯定要以某一屬性來作為突破口,它就是parent_uuid,那么到底是如何實(shí)現(xiàn)的 來看具體代碼

完整代碼

只貼重點(diǎn)代碼

首先使用了Mabatis-generator生成了通用后端代碼,結(jié)構(gòu)如下:

在這里插入圖片描述

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;


/**
 * 產(chǎn)品表
 *
 * @author
 * @date
 */

@RestController
@Api(tags = {"產(chǎn)品表"})
@RequestMapping("/caicai/product")
@Validated
public class ProductController {
    private static final Logger log = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private IProductBiz productBiz;

    /**
     * 產(chǎn)品樹
     */
    @ApiOperation(value = "產(chǎn)品樹")
    @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;
    /**
     * 根據(jù)條件查詢
     *
     * @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 = "產(chǎn)品表")
@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;
	/**
     * 父親 無父級(jí)為-1
     */
    @ApiModelProperty(value = "父親 無父級(jí)為-1", name = "parentUuid")
    private String parentUuid;
	/**
     * 產(chǎn)品層級(jí)
     */
    @ApiModelProperty(value = "產(chǎn)品層級(jí)", 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 = "產(chǎn)品表")
@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;
	/**
     * 父親 無父級(jí)為-1
     */
    @ApiModelProperty(value = "父親 無父級(jí)為-1", name = "parentUuid")
    private String parentUuid;
	/**
     * 產(chǎn)品層級(jí)
     */
    @ApiModelProperty(value = "產(chǎn)品層級(jí)", name = "level")
    private String level;
	/**
     *
     */
    @ApiModelProperty(value = "", name = "createTime")
    private Date createTime;

    @ApiModelProperty(value = "下屬產(chǎn)品", name = "subList")
    private List<ProductRsp> subList;
}

測試一下

在這里插入圖片描述

可以看到,實(shí)現(xiàn)了我們的效果

總結(jié)-核心代碼

上面羅里吧嗦,其實(shí)核心代碼就是以下代碼,親們來試著理解一下,然后就可以在此基礎(chǔ)上美化一下就好了:
ProductRsp、ProductReq 是實(shí)體類,可以自行替換里面的內(nèi)容

  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);
        });
    }

到此這篇關(guān)于JAVA如何把數(shù)據(jù)庫的數(shù)據(jù)處理成樹形結(jié)構(gòu)的文章就介紹到這了,更多相關(guān)JAVA如何把數(shù)據(jù)庫的數(shù)據(jù)處理成樹形結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Hibernate Validator實(shí)現(xiàn)更簡潔的參數(shù)校驗(yàn)及一個(gè)util

    Hibernate Validator實(shí)現(xiàn)更簡潔的參數(shù)校驗(yàn)及一個(gè)util

    這篇文章主要介紹了Hibernate Validator實(shí)現(xiàn)更簡潔的參數(shù)校驗(yàn)及一個(gè)util,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • SpringBoot整合Mybatis與thymleft實(shí)現(xiàn)增刪改查功能詳解

    SpringBoot整合Mybatis與thymleft實(shí)現(xiàn)增刪改查功能詳解

    MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動(dòng)。本文將整合MybatisPlus實(shí)現(xiàn)增刪改查功能,感興趣的可以了解一下
    2022-12-12
  • mybatis的if判斷不要使用boolean值的說明

    mybatis的if判斷不要使用boolean值的說明

    這篇文章主要介紹了mybatis的if判斷不要使用boolean值的說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 深入解析@InitBinder注解的功能與應(yīng)用

    深入解析@InitBinder注解的功能與應(yīng)用

    這篇文章主要介紹了深入解析@InitBinder注解的功能與應(yīng)用,從字面意思可以看出這個(gè)的作用是給Binder做初始化的,被此注解的方法可以對(duì)WebDataBinder初始化,webDataBinder是用于表單到方法的數(shù)據(jù)綁定的,需要的朋友可以參考下
    2023-10-10
  • Java集成swagger文檔組件

    Java集成swagger文檔組件

    這篇文章主要介紹了Java集成swagger文檔組件,通過詳細(xì)的圖文介紹和代碼分析展示,從頭展開說明詳細(xì)的過程,希望對(duì)你的開發(fā)有所幫助
    2021-06-06
  • Lombok的@Accessors使用說明

    Lombok的@Accessors使用說明

    這篇文章主要介紹了Lombok的@Accessors使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 詳解spring如何使用注解開發(fā)

    詳解spring如何使用注解開發(fā)

    今天給大家整理了spring如何使用注解開發(fā)這篇文章,文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Eclipse的Debug調(diào)試技巧大全(總結(jié))

    Eclipse的Debug調(diào)試技巧大全(總結(jié))

    這篇文章主要介紹了Eclipse的Debug調(diào)試技巧大全(總結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • sin(x)如何求解的java代碼實(shí)現(xiàn)方法

    sin(x)如何求解的java代碼實(shí)現(xiàn)方法

    這篇文章主要為大家介紹了sin(x)如何求解的java代碼實(shí)現(xiàn)方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • idea中導(dǎo)入項(xiàng)目后main方法無法Run的解決

    idea中導(dǎo)入項(xiàng)目后main方法無法Run的解決

    這篇文章主要介紹了idea中導(dǎo)入項(xiàng)目后main方法無法Run的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2023-03-03

最新評(píng)論

长葛市| 分宜县| 乌拉特前旗| 湖北省| 长沙县| 中江县| 青河县| 武夷山市| 通海县| 寿阳县| 长武县| 康定县| 新密市| 新津县| 阿坝| 公主岭市| 木里| 伊川县| 敦化市| 三江| 康平县| 定西市| 若羌县| 富民县| 连城县| 思南县| 永靖县| 澄迈县| 永新县| 昂仁县| 保德县| 项城市| 沙坪坝区| 休宁县| 尼玛县| 财经| 漠河县| 那坡县| 舒城县| 田林县| 正安县|