获取子父节点,tree树状结构的方式--通过循环的方式

1.导包

<dependencies>  //将domain quary写到了这层    <dependency>        <groupId>cn.xxx.aigou</groupId>        <artifactId>qqq_product_interface</artifactId>        <version>1.0-SNAPSHOT</version>    </dependency>

        <!--springboot相关的依赖-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>

        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>        </dependency>

        <!--mp的依赖-->        <dependency>            <groupId>com.baomidou</groupId>            <artifactId>mybatis-plus-boot-starter</artifactId>            <version>2.2.0</version>        </dependency>

        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.20</version>        </dependency>    <!--eureka的客户端-->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>    </dependency>

    <!--swagger2接口文档依赖-->    <dependency>        <groupId>io.springfox</groupId>        <artifactId>springfox-swagger2</artifactId>        <version>2.9.2</version>    </dependency>    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->    <dependency>        <groupId>io.springfox</groupId>        <artifactId>springfox-swagger-ui</artifactId>        <version>2.9.2</version>    </dependency>    <!--配置中心支持-->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-config</artifactId>    </dependency></dependencies>

2.contrlloer实现
/** * 树状结构数据查询: * @return */@RequestMapping(value = "/treeData",method = RequestMethod.GET)public List<ProductType> treeData(){

    return productTypeService.treeData();}
3.service写逻辑
@Overridepublic List<ProductType> treeData() {    /*        使用循环的方式获取不同层次的节点     */    return treeDataLoop();}private List<ProductType> treeDataLoop() {    //1:首先查询出所有的数据    List<ProductType> allProductTypes = productTypeMapper.selectList(null);

    //获取所有的数据的key--value的值    Map<Long,ProductType> map=new HashMap<>();    for (ProductType pt : allProductTypes) {        map.put(pt.getId(),pt);    }

    //最终返回的结果:一级菜单    List<ProductType> result=new ArrayList<>();

    //2:遍历:封装父节点和子节点    for (ProductType currentProductType : allProductTypes) {        //判断是否是一级菜单        if(currentProductType.getPid()==0){            // 如果是一级菜单则返回            result.add(currentProductType);        }else{            ProductType parent =map.get(currentProductType.getPid());            parent.getChildren().add(currentProductType);        }

    }

    return result;}
3.前台写好后,直接测试

原文地址:https://www.cnblogs.com/wgyi140724-/p/10634135.html

时间: 2024-12-16 01:36:44

获取子父节点,tree树状结构的方式--通过循环的方式的相关文章

.NET easyUI tree树状结构

简单的制作后台制作写一个json(string类型)格式 public partial class goodstype_type : System.Web.UI.Page { public string datalist = "",tree=""; goodstypeManage bll = new goodstypeManage(); goodstype model = new goodstype(); protected void Page_Load(objec

树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number o

js jquery 获取元素(父节点,子节点,兄弟节点)

一,js 获取元素(父节点,子节点,兄弟节点) var test = document.getElementById("test"); var parent = test.parentNode; // 父节点 var chils = test.childNodes; // 全部子节点 var first = test.firstChild; // 第一个子节点 var last = test.lastChile; // 最后一个子节点 var previous = test.previ

js jquery 获取元素(父节点,子节点,兄弟节点),元素筛选

转载:https://www.cnblogs.com/ooo0/p/6278102.html js jquery 获取元素(父节点,子节点,兄弟节点) 一,js 获取元素(父节点,子节点,兄弟节点) var test = document.getElementById("test"); var parent = test.parentNode; // 父节点 var chils = test.childNodes; // 全部子节点 var first = test.firstChil

jquery 获取元素(父节点,子节点,兄弟节点)

一, js 获取元素(父节点,子节点,兄弟节点)var test = document.getElementById("test"); var parent = test.parentNode; // 父节点 var chils = test.childNodes; // 全部子节点 var first = test.firstChild; // 第一个子节点 var last = test.lastChile; // 最后一个子节点 var previous = test.previ

Android无限级树状结构

通过对ListView简单的扩展.再封装,即可实现无限层级的树控件TreeView. 1 package cn.asiontang.nleveltreelistview; 2 3 import android.annotation.TargetApi; 4 import android.content.Context; 5 import android.os.Build; 6 import android.util.AttributeSet; 7 import android.view.View

优雅的处理树状结构——组合模式总结

1.前言 本模式经 遍历“容器”的优雅方法——总结迭代器模式 引出,继续看最后的子菜单的案例 2.组合模式的概念 组合模式,也叫 Composite 模式……是构造型的设计模式之一. 组合模式允许对象组合成树形结构,来表现“整体/部分”的层次结构,使得客户端对单个对象和组合对象的使用具有一致性. Composite Pattern Compose objects into tree structures to represent part-whole hierarchies. Composite

系统管理模块_部门管理_设计(映射)本模块中的所有实体并总结设计实体的技巧_懒加载异常问题_树状结构

系统管理模块_部门管理_设计本模块中的所有实体并总结设计实体的技巧 设计实体流程 1,有几个实体? 一般是一组增删改查对应一个实体. 2,实体之间有什么关系? 一般是页面引用了其他的实体时,就表示与这个实体有关联关系. 3,每个实体中都有什么属性? 1,主键.推荐使用代理主键 2,关联关系属性.在类图中,关联关系是一条线,有两端,每一端对应一个表达此关联关系的属性.有几个端指向本类,本类中就有几个关联关系属性. 3,一般属性.分析所有有关的页面,找出表单中要填写的或是在显示页面中要显示的信息等.

树状结构组装

后台数据组装 /** * 查询权限列表 * @param categoryId * @return */ @Override public List<Map<String, Object>> getMenus(String categoryId) { List<Map<String, Object>>menuallist= userDao.selectMenuAllList(); List<String> meunSelectedList = n