递归:
1:
2:实体类
@Data @NoArgsConstructor @AllArgsConstructor public class Department { private Integer id; private String name; private Integer pid; private Integer status; private String desc; /** * 子目录列表 */ private List<Department> treeNode; }
@RestController @RequestMapping("/department") @SuppressWarnings("all") //@RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class DepartmentController { /* private final DepartmentService departmentService; */ @Resource private DepartmentService departmentService; // @PostMapping("treeDepartment") // public Result treeDepartment(){ // List<Tree<String>> departmentAll = departmentService.findDepartmentAll(); // return new Result(ResultCode.SUCCESS,departmentAll); // } @PostMapping("treeDepartment") public Result treeDepartment(){ List<Department> departmentAll = departmentService.findDepartmentAll(); return new Result(ResultCode.SUCCESS,departmentAll); } }
4:service 层
@Service public interface DepartmentService { // List<Tree<String>> findDepartmentAll(); List<Department> findDepartmentAll(); }
实现类:主要逻辑
@Service public class DepartmentSericeImpl implements DepartmentService { @Resource private DepartmentMapper departmentMapper; private static final String status = "status"; private static final String desc = "desc"; // @Override // public List<Tree<String>> findDepartmentAll() { // List<Department> list = departmentMapper.findDepartmentAll(); // List<Department> list2 = CollUtil.newArrayList(); // //浅拷贝赋值 // list2.addAll(list); // // rootId // String pid = "0"; // //配置 // TreeNodeConfig nodeConfig = new TreeNodeConfig(); // // 自定义属性名 都要默认值的 // //设置ID对应的名称 // nodeConfig.setIdKey("id"); // // 最大递归深度 3级目录 // nodeConfig.setDeep(3); 入参 tree: 最终要返回的数据 node: lists数据 // 返回 Tree<String> Tree: 转换的实体 为数据源里的对象类型 String: ID类型 // //转换器 // List<Tree<String>> treeList = TreeUtil.build(list2, pid, nodeConfig, // (node, tree) -> { // //id // tree.setId(node.getId().toString()); // //姓名 // tree.setName(node.getName()); // //获取父节点id // tree.setParentId(node.getPid().toString()); // // 扩展的属性 ... // tree.putExtra(status, node.getStatus()); // tree.putExtra(desc, node.getDesc()); // }); // return treeList; // } @Override public List<Department> findDepartmentAll() { List<Department> departmentList = departmentMapper.findDepartmentAll(); //将最终地查询结果存放到这个新地集合中 List<Department> newDepartmentList = Lists.newArrayList(); List<Integer> stringArrayList = new ArrayList<>(); for (Department department : departmentList) { //存放部门编号 stringArrayList.add(department.getId()); } //判断查出来的数据是否为空 for (Iterator<Department> iterator = departmentList.iterator(); iterator.hasNext(); ) { Department department = iterator.next(); //判断departmentList 不包含部门父级id if (!stringArrayList.contains(department.getPid())) { recursionFn(departmentList, department); newDepartmentList.add(department); } } if (newDepartmentList.isEmpty()) { newDepartmentList = departmentList; } List<Department> finallDepartmentsList = newDepartmentList .stream() .filter(department -> { Integer pid = 0; return department.getPid().equals(pid); }).collect(Collectors.toList()); return finallDepartmentsList; } private void recursionFn(List<Department> departmentList, Department department) { List<Department> childList = getChildList(departmentList, department); department.setTreeNode(childList); //在遍历的到子节点的数据,判断子节点下是否还存有子节点 for (Department depart : childList) { //如果子节点中的数据依然拥有子节点,重新调用递归 if (hasChild(departmentList, depart)) { recursionFn(departmentList, depart); } } } private List<Department> getChildList(List<Department> list, Department t) { ArrayList<Department> tlist = new ArrayList<>(); //遍历集合中的数据 Iterator<Department> iterator = list.iterator(); while (iterator.hasNext()) { Department next = iterator.next(); if (!StringUtils.isEmpty(next.getPid()) && next.getPid().equals(t.getId())) { tlist.add(next); } } return tlist; } /** * 判断是否有子节点 */ private boolean hasChild(List<Department> list, Department t) { return getChildList(list, t).size() > 0; } }
5:mapper 层 以及xml文件
@Mapper public interface DepartmentMapper { List<Department> findDepartmentAll(); }
xml.
<select id="findDepartmentAll" resultType="com.example.demotest.pojo.Department"> select * from department
最终结果如下: