Java 递归查询树是很常见的功能,也有很多写法,小编这里记录stream流递归部门树写法,自从小编用上stream流之后,是爱不释手,的确是个不错的好东西,话不多说,直接上代码
第一步:先创建dept部门表,简单塞点数据
第二步:创建实体类
package com.example.work.entity;
import lombok.Data;
@Data
public class Dept {
/**
* 主键id
*/
private Integer id;
/**
* 部门名称
*/
private String deptName;
/**
* 父级id
*/
private Integer parentId;
}
第三步:创建部门vo对象
package com.example.work.vo;
import com.example.work.entity.Dept;
import lombok.Data;
import java.util.List;
/**
* @Description: 部门树vo
* @Author: lyz
* @CreateTime: 2024-01-11
*/
@Data
public class DeptTreeVO extends Dept {
private List<DeptTreeVO> childrenList;
}
第三步:编写controller层
package com.example.work.controller;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.work.service.DeptService;
import com.example.work.vo.DeptTreeVO;
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.RestController;
import java.util.List;
/**
* @Description: TODO
* @Author: lyz
* @CreateTime: 2024-01-11
*/
@RestController
@RequestMapping("dept")
public class DeptController {
@Autowired
DeptService deptService;
/**
* @Description: 部门树
* @return R
**/
@GetMapping("/getTreeList")
public R getTreeList(){
List<DeptTreeVO> treeVOList = deptService.getTreeList();
return R.ok(treeVOList);
}
}
第四步:业务层代码逻辑实现
package com.example.work.service.impl;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.work.dao.DeptMapper;
import com.example.work.entity.Dept;
import com.example.work.service.DeptService;
import com.example.work.vo.DeptTreeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @Description: TODO
* @Author: lyz
* @CreateTime: 2024-01-11
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
@Override
public List<DeptTreeVO> getTreeList() {
List<DeptTreeVO> deptTreeVOList = new ArrayList<>();
List<Dept> list = this.list();
list.forEach(dept -> {
DeptTreeVO deptTreeVO = new DeptTreeVO();
BeanUtils.copyProperties(dept, deptTreeVO);
deptTreeVOList.add(deptTreeVO);
});
//获取父部门信息
List<DeptTreeVO> deptTreeVOS = deptTreeVOList.stream().filter(x -> x.getParentId() == 0)
.map(deptTreeVO -> {
deptTreeVO.setChildrenList(getChildrenList(deptTreeVO, deptTreeVOList));
return deptTreeVO;
}).collect(Collectors.toList());
return deptTreeVOS;
}
/**
* @return List<DeptTreeVO> 子部门信息
* @Description: 递归查询子部门
* @param[1] root 父部门
* @param[2] deptTreeVOList 所有部门
**/
private List<DeptTreeVO> getChildrenList(DeptTreeVO root, List<DeptTreeVO> deptTreeVOList) {
List<DeptTreeVO> childrenList = deptTreeVOList.stream().filter(x -> {
return Objects.equals(x.getParentId(), root.getId());
}).map(deptTreeVO -> {
deptTreeVO.setChildrenList(getChildrenList(deptTreeVO, deptTreeVOList));
return deptTreeVO;
}).collect(Collectors.toList());
return childrenList;
}
}
第五步:启动程序,并调用接口,结果如下
stream流递归查询部门树,你学会了吗