@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. HDU - 1071 - The area - 高斯约旦消元法 - 自适应辛普森法积分

    http://acm.hdu.edu.cn/showproblem.php?pid=1071 解一个给定三个点的坐标二次函数某区域的积分值. 设出方程之后高斯消元得到二次函数.然后再消元得到直线. 两 ...

  2. MFC的CString使用

    CString没有基类.一个CString对象由可变长度的一队字符组成.CString使用类似于Basic的语法提供函数和操作符.连接和比较操作符以及简化的内存管理使CString对象比普通字符串数组 ...

  3. 如何使得 python 脚本 不一闪而过

    1.  简单的方法是在最后加上如下语句: os.system("pause") 2. 但是这个不一定有用,原因是可能在之前的代码中发生异常,那么我们看到的效果也是直接一闪而过 办法 ...

  4. php-fpm 高并发 参数调整

    工作中经常会遇到会给客户配置服务器,其中有的客户还会有并发量要求,其中也会必须要用负载均衡承载压力的.增加服务器数量肯定能有效的提升服务器承载能力,但只有根据目前已有配置设置好单台服务器才能更好的发挥 ...

  5. MyBatist庖丁解牛(三)

    从MyBatis代码实现的角度来看,MyBatis的主要的核心部件有以下几个: SqlSession:作为MyBatis工作的主要顶层API,表示和数据库交互的会话,完成必要数据库增删改查功能: Ex ...

  6. st表求区间最大值

    Input 第一行给出一个数字N,接下来N+1行,每行给出一个数字Ai,(0<=i<=N<=1E6)接来给出一个数字Q(Q<=7000),代表有Q个询问每组询问格式为a,b即询 ...

  7. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  8. 关于log

    如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...

  9. [題解]51nod_1515_明辨是非

    好久沒有話多了,是覺得有點浪費時間,今天考試和一中用的一樣的題,結果反而考得不好,不過Jackpei一句知恥而後勇點醒夢中人偷偷@Jackpei 就是這樣吧 還有我極度懷疑我的鍵帽打油了......我 ...

  10. $.ajax()与vue结合获取数据并渲染

    html: <div id="app1"> <ul> <li v-for="item in datas"> <div ...