@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. iOS 中使用 MJExtension 遇到 关键字(id) 怎么办

    MJExtension 是个人比较喜欢用的json 转model 的软件,当遇到系统关键字时就会出现崩溃,解决方式如下 1.建立Modle 解析类,服务返回数据中带有id,这个时候用字典转Mode(m ...

  2. JAG Practice Contest for ACM-ICPC Asia Regional 2016 C题【贪心】

    camp给出的题解: 题解:贪心,先算出最小需要的长度.然后从左到右依次确定每一位.复杂度O(n)O(n) 长度为 2n2n 的串可以构造出需要 [0,1+3+...+2n-1][0,1+3+...+ ...

  3. Android studio改包名

    http://www.cnblogs.com/Kyouhui/p/4632813.html Android Studio,咱们开发安卓的利器,自推出就受到移动开发者的追捧,但一路走来,大家谈到他,充满 ...

  4. UGUI实现unity摇杆

    http://www.winig.cc/archives/348 好久没有写文章了,最近在做项目是用的unity最新的ui系统UGUI,项目需要做一个摇杆,网上大部分都是用的插件和NGUI做的摇杆,u ...

  5. 51nod1247(gcd)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1247 题意:中文题诶- 思路:(a, b)可以直接到达(a+b ...

  6. IDEA快捷的添加包名

    Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题 2018年02月24日 17:24:49 _云卷云舒_ 阅读数:6264 标签: intellij idea 更 ...

  7. SpringBlade 2.0-RC3 发布,全新的微服务开发平台

    经过了十天的艰苦奋斗,SpringBlade 2.0-RC3发布了,此版本增加了很多有用的功能,距离正式版本更近一步! SpringBlade简介: SpringBlade 2.0 是一个基于 Spr ...

  8. web.xml中一个filter配置多个url-pattern

    需要在filter标签后添加多个filter-mapping标签,一个url-pattern就对应一个filter-mapping标签,不能直接把多个url-pattern配置到同一个filter-m ...

  9. 线段树-区间更新-HDU 1689

    #include <iostream> #include <cstdio> #include <string> #include <cstring> # ...

  10. 网络流--Dinic(自用,勿看)

    注意:这是一篇个人学习笔记,如果有人因为某些原因点了进来并且要看一下,请一定谨慎地阅读,因为可能存在各种奇怪的错误,如果有人发现错误请指出谢谢! https://www.luogu.org/probl ...