可以使用JSON格式的数据,一层一层包裹
{ BigName: '''', code :'', name:[ { shortCode:'''' shortName: ''' } ]
@ApiModelProperty(value = "科室编号") private String depcode; @ApiModelProperty(value = "科室名称") private String depname; @ApiModelProperty(value = "下级节点") private List<DepartmentVo> children; }
ArrayList<DepartmentVo> result = new ArrayList<>(); //查询所有的科室信息 Department department = new Department(); department.setHoscode(hoscode); Example<Department> example = Example.of(department); List<Department> departmentList = departmentRepository.findAll(example); //首先给根据把大科室 按照大科室编号进行分组(bigcode),在获取每个大科式里面的小科室 Map<String, List<Department>> departmentMap = departmentList.stream() .collect(Collectors.groupingBy(Department::getBigcode)); //遍历map 集合 for(Map.Entry<String,List<Department>> entry : departmentMap.entrySet()){ //大科室 的编号 String bigCOde = entry.getKey(); //大科室 的编号 对应的全部数据 List<Department> departmentsList = entry.getValue(); //封装大科室 DepartmentVo departmentVo = new DepartmentVo(); departmentVo.setDepcode(bigCOde); departmentVo.setDepname(departmentsList.get(0).getDepname()); //封装小科室 大科室里面有很多的小科室 ArrayList<DepartmentVo> children = new ArrayList<>(); for (Department departments : departmentsList){ DepartmentVo departmentVo1 = new DepartmentVo(); departmentVo1.setDepcode(departments.getDepcode()); departmentVo1.setDepname(departments.getDepname()); //封装到小科室里 children.add(departmentVo1); } //把小科室的信息放到大科室 里 departmentVo.setChildren(children); //最终放到result 里面 result.add(departmentVo); } return result;