实体类:

public class AddressTreeDto {
private Long id;
private String text;//位置名称
private Long pId;//上一级
private Integer able; private List<AddressTreeDto> nodes; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} public Long getpId() {
return pId;
} public void setpId(Long pId) {
this.pId = pId;
} public Integer getAble() {
return able;
} public void setAble(Integer able) {
this.able = able;
} public List<AddressTreeDto> getNodes() {
return nodes;
} public void setNodes(List<AddressTreeDto> nodes) {
this.nodes = nodes;
}
}

Service接口:

public interface AddressService {
//查询所有存放地,返回TreeView数据格式 List<AddressTreeDto> getAddressTree();
}

ServiceImpl实现类

public class AddressServiceImpl implements AddressService {
@Autowired
private AddressMapper addressMapper;
private List<AddressTreeDto> getChild(Long id, List<AddressTreeDto> rootAddress) {
// 子菜单
List<AddressTreeDto> childList = new ArrayList<>();
for (AddressTreeDto treeDto : rootAddress) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (treeDto.getpId()!=null) {
if (treeDto.getpId().equals(id)) {
childList.add(treeDto);
}
}
}
// 把子菜单的子菜单再循环一遍
for (AddressTreeDto treeDto: childList) {
// 没有url子菜单还有子菜单---判断还有子菜单
if(getIds(treeDto.getId())!=null){
//递归
treeDto.setNodes(getChild(treeDto.getId(),rootAddress));
}
} // 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}
@Override
public List<AddressTreeDto> getAddressTree() {
// 原始的数据
List<AddressTreeDto> rootAddress = addressMapper.selectTree(Constants.ABLE_CONFIG.DEFAULT_ABLE);
// 查看结果
for (AddressTreeDto treeDto1 : rootAddress) {
System.out.println(treeDto1);
}
// 最后的结果
List<AddressTreeDto> addressList = new ArrayList<>();
// 先找到所有的一级菜单
for (int i = 0; i < rootAddress.size(); i++) {
// 一级菜单没有parentId
if (rootAddress.get(i).getpId()==0) {
addressList.add(rootAddress.get(i));
}
}
// 为一级菜单设置子菜单,getChild是递归调用的
for (AddressTreeDto treeDto1 : addressList) {
treeDto1.setNodes(getChild(treeDto1.getId(), rootAddress));
}
return addressList; } }

查询部门----返回给前台TreeView数据格式的数据的更多相关文章

  1. js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中

    ajaxfileupload.js jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId ...

  2. <MyBatis>入门五 查询的返回值处理

    select : 返回对象:  <select  id = " "  resultType= "对象的全类名"  /> List: <sele ...

  3. Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据

    Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据 1. 1. 配置条件字段@Conditional 1 1 2. 2. 配置条件字段 ...

  4. 部门树形结构,使用Treeview控件显示部门

    部门树形结构.设计张部门表用于存储部门编码.名称.上级部门id,使用Treeview控件显示部门树,并实现部门增删改.移动.折叠等功能.特别提示,部门有层级关系,可用donetbar的adtree控件 ...

  5. PDO 查询mysql返回字段整型变为String型解决方法

    PDO 查询mysql返回字段整型变为String型解决方法 使用PDO查询mysql数据库时,执行prepare,execute后,返回的字段数据全都变为字符型. 例如id在数据库中是Int的,查询 ...

  6. **CI两种方式查询所返回的结果数量

    区别:第一个是有条件的:第二个没有条件 $this->db->count_all_results(); 允许你获得某个特定的Active Record查询所返回的结果数量.可以使用Acti ...

  7. Atitit.列表页and查询条件的最佳实践(1)------设定搜索条件and提交查询and返回json数据

    Atitit.列表页and查询条件的最佳实践(1)------设置查询条件and提交查询and返回json数据 1. 1. 配置条件字段@Conditional 1 1 2. 2. 配置条件字段显示类 ...

  8. Linq to Sql:N层应用中的查询(上) : 返回自定义实体

    原文:Linq to Sql:N层应用中的查询(上) : 返回自定义实体 如果允许在UI层直接访问Linq to Sql的DataContext,可以省去很多问题,譬如在处理多表join的时候,我们使 ...

  9. Hibernate SQLQuery 原生SQL 查询及返回结果集处理-1

    第一篇:官方文档的处理方法,摘自官方 在迁移原先用JDBC/SQL实现的系统,难免需要采用hibernat native sql支持. 1.使用SQLQuery hibernate对原生SQL查询执行 ...

随机推荐

  1. GUI_文件管理器(练习)

    实现想windows下的文件管理器(主要是监听器里的方法,showDir()写法) package com.mywindow.test; import java.awt.event.ActionEve ...

  2. vue.js 防暴力点击方案

    import lodash from 'lodash' <input v-on:onclick ="doStuff">methods: { doStuff:loadsh ...

  3. Python-2.7 : 编码问题及encode与decode

    普通的字符串在py2.7中都是以ASCII编码的,例如str=“abc”,若含有中文则会以gbk或者gb2312编码(GB2312是中国规定的汉字编码,也可以说是简体中文的字符集编码;GBK 是 GB ...

  4. 【Codeforces 1109C 】Sasha and a Patient Friend

    Codeforces 1109 C 题意:现在有个碗,每时每刻其中的水量都会加一个整数(可以为负). 给\(n\)个询问,询问有\(3\)种类型: \(1\ t\ s\):将从第\(t\)秒开始水量增 ...

  5. ThreadGroup其实比ExecutorService更好

    用java做抓取的时候免不了要用到多线程的了,因为要同时抓取多个网站或一条线程抓取一个网站的话实在太慢,而且有时一条线程抓取同一个网站的话也比较浪费CPU资源.要用到多线程的等方面,也就免不了对线程的 ...

  6. JAVA中map的分类和各自的特性

    java为数据结构中的映射定义了一个接口java.util.Map,他实现了四个类,分别是:HashMap,HashTable,LinkedHashMapTreeMap Map不允许键重复,但允许值重 ...

  7. MYSQL表情存储数据库报错

    1.windows安装5.5.5.9的服务器 2.打开window服务找到mysql的服务,右键属性查看路径 3.打开路径,修改mysql配置文件my.ini 4.修改[client]中的defaul ...

  8. CMD命令查看当前电脑安装所有版本.NET Core SDK(转载)

    dotnet --version 查看当前使用版本 dotnet --info 安装的所有版本 包括版本地址 也可用命令帮助 dotnet help 原文链接

  9. Linux集锦

    一:Linux文件系统 Linux系统有一个重要概念:一切都都式文件. Linux支持五种文件类型: Linux的目录结构如下: 常见目录说明: /bin: 存放二进制可执行文件(ls,cat,mkd ...

  10. Luogu4652 CEOI2017 One-Way Streets 树上差分

    传送门 题意:给出$N$个点.$M$条无向边的图,现在你需要给它定向,并满足$Q$个条件:每个条件形如$(x_i,y_i)$,表示定向之后需要存在路径从$x_i$走向$y_i$.问每条边是否都有唯一定 ...