Java递归获取部门树 返回jstree数据
@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数据的更多相关文章
- 根据租户id获取部门树状结构有父子结构的数据list
/** * 根据租户id获取部门树状结构 * @param tenantId * @return */ @GetMapping("getDeptTreeList") public ...
- 如何用 ajax 连接mysql数据库,并且获取从中返回的数据。ajax获取从mysql返回的数据。responseXML分别输出不同数据的方法。
开讲前,先说下网上,大部分的关于这方面的博文或者其他什么的,就我自己的感觉,第一说得不详细,第二语言不能很好的被初学者了解. 我这篇博文的标题之所以用了三句,是为了方便其他人好查找: 这里介绍的方法有 ...
- Java 递归获取一个路径下的所有文件,文件夹名称
package com.readfile; import java.io.File; public class GetAllFiles { public static void main(String ...
- Mybatis通过colliection属性递归获取菜单树
1.现有商品分类数据表category结构如下,三个字段都为varchar类型 2.创建商品分类对应的数据Bean /** * */ package com.xdw.dao; import java. ...
- PHP递归实现层级树状展现数据
树状数据展现很常用,今天学习了PHP的递归,也来总结总结! PHP代码: function _getTreeList_custom1($data,$parent_id,$depth) { $retur ...
- java 递归获取一个目录下的所有文件路径
还是日志的问题,log4j生成的日志文件,自动保存到月份所在的文件夹中,需要获取到所有的日志文件,包括文件夹 private List<String> ergodic(File file, ...
- 根据ID和parentID利用Java递归获取全路径名称
如下图所示,本文参考资源:https://jie-bosshr.iteye.com/blog/1996607 感谢大佬的无私奉献. 思路: 定义一个方法getParentName参数为int类型的c ...
- 使用RestTemplate请求报出HttpClientErrorException异常并获取不到返回body数据
描述: 使用RestTemplate请求url,由于Token等验证信息参数失效,报出 401 HttpClientErrorException异常.并且获取不到body消息体的错误信息.然而post ...
- PHP 递归实现层级树状展现数据
<?php $db = mysql_connect('localhost', 'root', 'root') or die('Can\'t connect to database'); mysq ...
随机推荐
- 洛谷 - P2281 - 多项式的加法和乘法 - 大模拟
题目链接:https://www.luogu.org/problemnew/show/P2281 题目的意思很简单,输入两个系数.指数都是整数,变量都是大写字母的多项式,求他们的加法结果和乘法结果. ...
- POJ3737【数学】
高中数学题?初中吧///然后注意一下POJ的double输出用%f.......... #include <iostream> #include <stdio.h> #incl ...
- Unity(2) 脚本简单操作
生命周期(按顺序排列) Awake():脚本唤醒,系统执行的第一个方法,在脚本声明周期内只执行一次,初始化一般可以在这里 Start():Awake之后,Update之前,只执行一次,一般在awake ...
- mongodb 由于计算机死机造成的无法启动故障
一次计算机死机,重启后,mongodb无法启动,log显示: exception in initAndListen: 12596 old lock file, terminating Sun Mar ...
- 监控利器---Zabbix(一)
开源监控软件对比 Cacti(英文含义仙人掌) 是一套基于PHP.MySQL.SNMP和RRDtool开发的网络流量监测图形分析工具,它通过snmpget来获取数据使用RRDtool绘图,简化RRDt ...
- ES6函数参数默认值作用域的模拟原理实现与个人的一些推测
一.函数参数默认值中模糊的独立作用域 我在ES6入门学习函数拓展这一篇博客中有记录,当函数的参数使用默认值时,参数会在初始化过程中产生一个独立的作用域,初始化完成作用域会消失:如果不使用参数默认值,不 ...
- bzoj2502清理雪道
传送门 好题啊,由于每个点既可以进,也可以出,就可以新建一个源点和汇点,对于每个点都连边,然后就是最小流板子了. 代码: #include<cstdio> #include<iost ...
- Git本地分支与远程分支关联
当clone完版本库,切换到开发分支后,使用git pull -r 拉取并合并分支之后会出现一下提示: $ git pull -rFrom ssh://192.168.1.226:29418/etha ...
- ZooKeeper理论知识
前言 相信大家对 ZooKeeper 应该不算陌生.但是你真的了解 ZooKeeper 是个什么东西吗?如果别人/面试官让你给他讲讲 ZooKeeper 是个什么东西,你能回答到什么地步呢? 我本人曾 ...
- Linux--NiaoGe-Service-08(路由)
路由 Linux系统下的路由表是由小到大排列的,即C类地址-->B类地址-->A类地址-->0.0.0.0(默认路由). Linux系统中使用route命令查看路由表 [root@w ...