首页 > 基础资料 博客日记
java获取部门树的方法实例
2023-08-04 16:46:37基础资料围观222次
Java资料网推荐java获取部门树的方法实例这篇文章给大家,欢迎收藏Java资料网享受知识的乐趣
开发中如何获取一个树结构是非常常见的需求,本示例演示一下该功能如何实现。
1、数据准备(mysql)
CREATE TABLE `dept` ( `dept_id` int NOT NULL AUTO_INCREMENT COMMENT 'id值', `parent_id` int DEFAULT NULL COMMENT '上级部门', `dept_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '名称', `sort` int DEFAULT NULL COMMENT '排序', PRIMARY KEY (`dept_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=212 DEFAULT CHARSET=utf8; INSERT INTO `dept` VALUES ('1', '0', '成都总公司', '1'); INSERT INTO `dept` VALUES ('2', '0', '宜宾分公司', '2'); INSERT INTO `dept` VALUES ('10', '1', '研发部门', '1'); INSERT INTO `dept` VALUES ('11', '1', '市场部门', '2'); INSERT INTO `dept` VALUES ('12', '1', '运维部门', '3'); INSERT INTO `dept` VALUES ('21', '2', '营销部门', '1'); INSERT INTO `dept` VALUES ('22', '2', '运维部门', '2'); INSERT INTO `dept` VALUES ('23', '2', '公关部门', '3'); INSERT INTO `dept` VALUES ('210', '21', '小组1', '2'); INSERT INTO `dept` VALUES ('211', '21', '小组2', '1');
2、工程结构
核心的类就是如下标红的类
3、核心源码
package com.example.demo.unit; import java.util.List; //实现Comparable接口,重写其CompareTo方法 public class Tree<T> implements Comparable { String id; String name; String parentId; List<T> children; private Integer order; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public List<T> getChildren() { return children; } public void setChildren(List<T> children) { this.children = children; } /** * @param order * the order to set */ public void setOrder(Integer order) { this.order = order; } /** * @return the order */ public Integer getOrder() { return order; } //定义排序规则 @Override public int compareTo(Object o) { Tree tree =(Tree) o; return this.getOrder().compareTo(tree.getOrder()); //这里定义你排序的规则。 } @Override public String toString() { return "Tree{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", parentId='" + parentId + '\'' + ", children=" + children + '}'; } }
package com.example.demo.unit; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class TreeUtils { //构建部门树方法 没有排序 public static List buildTreeNoSort(List<Tree> trees, String parentId) { //将每条数据的子节点添加进去 for (Tree tree : trees) { for (Tree tree1 : trees) { if (tree1.getParentId().equals(tree.getId())) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList()); } tree.getChildren().add(tree1); } } } System.out.println(trees); //把父节点是根节点的数据过滤出来就是最后的树 return trees.stream().filter(tree -> tree.getParentId().equals(parentId)).collect(Collectors.toList()); } //构建部门树方法 有排序 public static List buildTreeSort(List<Tree> trees, String parentId) { //将每条数据的子节点添加进去 for (Tree tree : trees) { for (Tree tree1 : trees) { if (tree1.getParentId().equals(tree.getId())) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList()); } tree.getChildren().add(tree1); //排序 Collections.sort(tree.getChildren()); } } } System.out.println(trees); //把父节点是根节点的数据过滤出来就是最后的树 return trees.stream().filter(tree -> tree.getParentId().equals(parentId)).collect(Collectors.toList()); } }
package com.example.demo.controller; import com.example.demo.domain.Dept; import com.example.demo.mapper.DeptMapper; import com.example.demo.unit.Tree; import com.example.demo.unit.TreeUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/hello") public class HelloController { @Autowired private DeptMapper deptMapper; //获取部门树,没有排序 @GetMapping("/getTreeNoSort") @ResponseBody public List<Tree> getTreeNoSort() { List<Dept> list = deptMapper.selectList(null); List<Tree> treeList = new ArrayList<Tree>(); //这样写避免在循环里写Tree tree = new Tree(); Tree tree = null; for(int i=0;i<list.size();i++){ Dept dept = list.get(i); //这种写法 仅在内存中保存一份对该对象的引用,而不像Tree tree = new Tree(); 写在循环里会在内存中产生大量的对象引用,浪费大量的内存空间,而且增大了垃圾回收的负荷。 tree = new Tree(); tree.setId(dept.getDeptId()); tree.setName(dept.getDeptName()); tree.setParentId(dept.getParentId()); treeList.add(tree); } List<Tree> treeDemo = TreeUtils.buildTreeNoSort(treeList,"0"); return treeDemo; } //获取部门树有排序功能 @GetMapping("/getTreeSort") @ResponseBody public List<Tree> getTreeSort() { List<Dept> list = deptMapper.selectList(null); List<Tree> treeList = new ArrayList<Tree>(); Tree tree = null; for(int i=0;i<list.size();i++){ Dept dept = list.get(i); tree = new Tree(); tree.setId(dept.getDeptId()); tree.setName(dept.getDeptName()); tree.setParentId(dept.getParentId()); tree.setOrder(dept.getSort()); treeList.add(tree); } List<Tree> treeDemo = TreeUtils.buildTreeSort(treeList,"0"); return treeDemo; } }
4、测试
(1)测试没有排序的部门树
http://localhost:8080/hello/getTreeNoSort
[ { "id": "1", "name": "成都总公司", "parentId": "0", "children": [ { "id": "10", "name": "研发部门", "parentId": "1", "children": null, "order": null }, { "id": "11", "name": "市场部门", "parentId": "1", "children": null, "order": null }, { "id": "12", "name": "运维部门", "parentId": "1", "children": null, "order": null } ], "order": null }, { "id": "2", "name": "宜宾分公司", "parentId": "0", "children": [ { "id": "21", "name": "营销部门", "parentId": "2", "children": [ { "id": "210", "name": "小组1", "parentId": "21", "children": null, "order": null }, { "id": "211", "name": "小组2", "parentId": "21", "children": null, "order": null } ], "order": null }, { "id": "22", "name": "运维部门", "parentId": "2", "children": null, "order": null }, { "id": "23", "name": "公关部门", "parentId": "2", "children": null, "order": null } ], "order": null } ]
(2)有排序的部门树
http://localhost:8080/hello/getTreeSort
这里使用了排序算法功能,原始数据如下,小组2排在了小组1前面。
INSERT INTO `dept` VALUES ('210', '21', '小组1', '2');
INSERT INTO `dept` VALUES ('211', '21', '小组2', '1');
[ { "id": "1", "name": "成都总公司", "parentId": "0", "children": [ { "id": "10", "name": "研发部门", "parentId": "1", "children": null, "order": 1 }, { "id": "11", "name": "市场部门", "parentId": "1", "children": null, "order": 2 }, { "id": "12", "name": "运维部门", "parentId": "1", "children": null, "order": 3 } ], "order": 1 }, { "id": "2", "name": "宜宾分公司", "parentId": "0", "children": [ { "id": "21", "name": "营销部门", "parentId": "2", "children": [ { "id": "211", "name": "小组2", "parentId": "21", "children": null, "order": 1 }, { "id": "210", "name": "小组1", "parentId": "21", "children": null, "order": 2 } ], "order": 1 }, { "id": "22", "name": "运维部门", "parentId": "2", "children": null, "order": 2 }, { "id": "23", "name": "公关部门", "parentId": "2", "children": null, "order": 3 } ], "order": 2 } ]
文章来源:https://www.cnblogs.com/yclh/p/16524457.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: