@GetMapping("/getDept")
@ResponseBody
public Tree<DeptDO> getDept(String deptId){
Tree<DeptDO> deptNode = getDeptNode(deptId);
if (deptNode == null){
return null;
}
List<Tree<DeptDO>> childNode = getChildNode(deptId);
for (Tree<DeptDO> child:childNode) {
Tree<DeptDO> node = getDept(child.getId());
deptNode.getChildren().add(node);
deptNode.setChildren(true);
}
return deptNode;
}
@Autowired
private DeptDao sysDeptMapper;
private Tree<DeptDO> getDeptNode(String deptId){
Map<String,Object> query = new HashMap<>();
query.put("deptId",deptId);
List<DeptDO> deptList =sysDeptMapper.list(query);
if (deptList.size() == 1){
DeptDO sysDept = deptList.get(0);
Tree<DeptDO> tree = getDeptTree(sysDept);
return tree;
}else{
return null;
}
}
private List<Tree<DeptDO>> getChildNode(String deptId){
List<Tree<DeptDO>> trees = new ArrayList<>();
Map<String,Object> query = new HashMap<>();
query.put("parentId",deptId);
List<DeptDO> sysDepts = sysDeptMapper.list(query);
for (DeptDO sysDept : sysDepts) {
Tree<DeptDO> tree = getDeptTree(sysDept);
trees.add(tree);
}
return trees;
}
private Tree<DeptDO> getDeptTree(DeptDO sysDept){
Tree<DeptDO> tree = new Tree<>();
tree.setId(sysDept.getDeptId().toString());
tree.setParentId(sysDept.getParentId().toString());
tree.setText(sysDept.getName());
Map<String, Object> state = new HashMap<>(16);
state.put("opened", true);
tree.setState(state);
if (!("0".equals(tree.getParentId()))){
tree.setHasParent(true);
}
return tree;
}
//树结构
public class Tree<T> {
private String id;//节点ID
private String text;//显示节点文本
private Map<String, Object> state;//节点状态,open closed
private boolean checked = false;//节点是否被选中 true false
private Map<String, Object> attributes;//节点属性
private List<Tree<T>> children = new ArrayList<Tree<T>>();//节点的子节点
private String parentId;//父ID
private boolean hasParent = false;//是否有父节点
private boolean hasChildren = false;//是否有子节点
//省略getter setter方法
} //第2种实现
@Override
public List<Tree> getTree() {
List<Tree> trees = new ArrayList();
List<DeptDO> list = deptDao.list(new HashMap<>());
for (DeptDO d : list) {
if (d.getParentid() == null) {
Tree tree = new Tree();
tree.setId(d.getDeptid().toString());
tree.setText(d.getDeptname());
Map state = new HashMap();
state.put("opened", true);
tree.setState(state);
List<Tree> childTree = getChild(d.getDeptid());
if (childTree.size() > 0) {
tree.setChildren(childTree);
}
trees.add(tree);
}
}
return trees;
} private List<Tree> getChild(Long parentid) {
List<Tree> trees = new ArrayList();
Map map = new HashMap();
map.put("parentid", parentid);
List<DeptDO> childList = deptDao.list(map);
for (DeptDO d : childList) {
Tree tree = new Tree();
tree.setId(d.getDeptid().toString());
tree.setText(d.getDeptname());
List<Tree> childTree = getChild(d.getDeptid());
if (childTree.size() > 0) {
tree.setChildren(childTree);
}
trees.add(tree);
}
return trees;
} //jstree json
public class Tree {
private String id;
private String text;
private String icon;
private Map state;
private List<Tree> children;
   //省略getter setter方法
}

Java递归获取部门树 返回jstree数据的更多相关文章

  1. 根据租户id获取部门树状结构有父子结构的数据list

    /** * 根据租户id获取部门树状结构 * @param tenantId * @return */ @GetMapping("getDeptTreeList") public ...

  2. 如何用 ajax 连接mysql数据库,并且获取从中返回的数据。ajax获取从mysql返回的数据。responseXML分别输出不同数据的方法。

    开讲前,先说下网上,大部分的关于这方面的博文或者其他什么的,就我自己的感觉,第一说得不详细,第二语言不能很好的被初学者了解. 我这篇博文的标题之所以用了三句,是为了方便其他人好查找: 这里介绍的方法有 ...

  3. Java 递归获取一个路径下的所有文件,文件夹名称

    package com.readfile; import java.io.File; public class GetAllFiles { public static void main(String ...

  4. Mybatis通过colliection属性递归获取菜单树

    1.现有商品分类数据表category结构如下,三个字段都为varchar类型 2.创建商品分类对应的数据Bean /** * */ package com.xdw.dao; import java. ...

  5. PHP递归实现层级树状展现数据

    树状数据展现很常用,今天学习了PHP的递归,也来总结总结! PHP代码: function _getTreeList_custom1($data,$parent_id,$depth) { $retur ...

  6. java 递归获取一个目录下的所有文件路径

    还是日志的问题,log4j生成的日志文件,自动保存到月份所在的文件夹中,需要获取到所有的日志文件,包括文件夹 private List<String> ergodic(File file, ...

  7. 根据ID和parentID利用Java递归获取全路径名称

    如下图所示,本文参考资源:https://jie-bosshr.iteye.com/blog/1996607  感谢大佬的无私奉献. 思路: 定义一个方法getParentName参数为int类型的c ...

  8. 使用RestTemplate请求报出HttpClientErrorException异常并获取不到返回body数据

    描述: 使用RestTemplate请求url,由于Token等验证信息参数失效,报出 401 HttpClientErrorException异常.并且获取不到body消息体的错误信息.然而post ...

  9. PHP 递归实现层级树状展现数据

    <?php $db = mysql_connect('localhost', 'root', 'root') or die('Can\'t connect to database'); mysq ...

随机推荐

  1. 51nod 1099【贪心】

    思路: 我们可以思考对于仅仅两个元素来说,A,B; 先选A的话是会  A.b+B.a; 先选B的话是会 B.b+A.a; 所以哪个小哪个就放前面; #include <cstdio> #i ...

  2. Lightoj1015【基础题】

    题意: 计算输入数>0的所有和: 思路: 直接干... #include<cstdio> #include<queue> #include<map> #inc ...

  3. Lightoj1002 【搜索】

    题意: 两两之间的点的花费就是:从A点到B的一条路上某段的最大权值:给一个起点,求到各起点的最小花费. 思路: 一开始的思路: n不是才500,我先建个图,然后DFS一下,不对,是2500: 如果直接 ...

  4. 集中化管理平台 — Ansible 详解

    # Ansible 简介 Ansible 类似于Saltstack,是一种集成IT系统的配置管理.应用部署.执行特定任务的开源平台.Ansible基于Python语言实现,由Paramiko和PyYA ...

  5. 密码破解工具John the Ripper使用说明

    John the Ripper John 包描述 John the Ripper 既功能丰富又运行快速. 它在一个程序中结合了几种破解模式,并且可以根据您的特定需求进行全面地配置(你甚至可以使用支持C ...

  6. 第十九篇 .NET高级技术之C#中的线程(一)

    原文://http://www.cnblogs.com/miniwiki/archive/2010/06/18/1760540.html 文章系参考转载,英文原文网址请参考:http://www.al ...

  7. luoguP3796[模板]AC自动机(加强版)

    传送门 ac自动机模板,可能我写的ac自动机是有点问题的,所以跑的有些慢 暴力跳fail统计 代码: #include<cstdio> #include<iostream> # ...

  8. 关于idea中使用lamb表达式报错:ambda expressions are not supported at this language level

    我使用的是jdk1.8,使用lamb表达式的时候,报错 ambda expressions are not supported at this language level, 后来,设置了 接着重启了 ...

  9. Jquery多选框互相内容交换

    <head runat="server"> <title>无标题页</title> <script type="text/jav ...

  10. Spring使用_进阶

    概述 本文主要写了几个关于Spring Aware,多线程,计划任务(定时任务),条件注解,组合注解,元注解,Spring测试的小例子以及关于@Enable*注解的工作原理的理解. Spring Aw ...