递归查询父级节点
1.一个节点只能有一个父级节点。
2.首先根据参数传递的id属性查询到pid为这个id的对象a,然后再获取这个刚查询的对象a的id,再查询pid为对象a的id的对象b,以此类推。
List<Long> result1 = new ArrayList<Long>();
public void selectFather(Long id){
//查询数据库中对应id的实体类
SysEnterpriseOrg sysEnterpriseOrg = sysEnterpriseOrgMapper.selectById(id);
//查询父级架构
//此处使用mybaatisPlus的条件构造器,查询id等于pid的对象
QueryWrapper<SysEnterpriseOrg> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<SysEnterpriseOrg>();
sysEnterpriseOrgChildQueryWrapper.eq("id",sysEnterpriseOrg.getPid());
//查询出符合条件的对象
sysEnterpriseOrg= sysEnterpriseOrgMapper.selectOne(sysEnterpriseOrgChildQueryWrapper);
if(sysEnterpriseOrg!=null){
//如果查出的对象不为空,则将此对象的id存到全局变量中,并且继续调用自己,即递归,一直到查询不到为止
result1.add(sysEnterpriseOrg.getId());
selectFather(sysEnterpriseOrg.getId());
递归查询子级节点
1.一个节点可能有多个子级节点,每个自己节点可能还有更多的子级节点。
2.所以递归时的参数用一个list来接受,首先遍历参数list,分别查询pid为参数id的对象。
3.每一个参数id所查询返回的数据是一个对象的list。
4.遍历list获取符合条件的对象的id值,一份存到temp中用作递归的参数,并存到全局变量中用来获取所有符合条件的id。
List<Long> result = new ArrayList<Long>();
public void selectChild(List<Long> ids){
//用来存取调用自身递归时的参数
List<Long> temp= new ArrayList<Long>();
//查询数据库中对应id的实体类
List<SysEnterpriseOrg> sysEnterpriseOrgList = new ArrayList<SysEnterpriseOrg>();
//遍历传递过来的参数ids
for (Long id :ids) {
//查询子级架构
//此处使用mybaatisPlus的条件构造器,查询pid等于id的对象
QueryWrapper<SysEnterpriseOrg> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<SysEnterpriseOrg>();
sysEnterpriseOrgChildQueryWrapper.eq("pid",id.toString());
//查询结果返回一个list
sysEnterpriseOrgList= sysEnterpriseOrgMapper.selectList(sysEnterpriseOrgChildQueryWrapper);
//遍历list获取符合条件的对象的id值,一份存到temp中用作递归的参数,并存到全局变量中用来获取所有符合条件的id
for (SysEnterpriseOrg s:sysEnterpriseOrgList) {
temp.add(s.getId());
result.add(s.getId());
if(temp.size()!=0&&temp!=null){
selectChild(temp);
场景在企业架构管理中使用树形结构进行管理,如图:注:如果A的id是B的pid,那么A就是B的父级。数据库数据如下:现在需要根据传递的id查询此节点所有的父级节点以及此节点所有的子级节点。实现递归查询父级节点1.一个节点只能有一个父级节点。2.首先根据参数传递的id属性查询到pid为这个id的对象a,然后再获取这个刚查询的对象a的id,再查询pid为对象a的i...
public class getChildren {
static List<Map<String, Object>> childCategoryList = new ArrayList<Map<String, Object>>();
public sta...
// 装子节点集合
static List<TbUserDepartmentDic> childMenu=new ArrayList<TbUserDepartmentDic>();
递归查询全部的子节点,子节点下也有子节点额
menuList 全部的集合
您可以使用递归算法实现树形结构的父子级查询。首先,您需要根据节点的ID和parentID来构建树形结构,然后根据节点的ID查询其所有子级,或者根据子节点的ID查询其父节点。以下是Java代码示例:
// 树形结构节点类
public class TreeNode {
private String id;
private String parentId;
private List<TreeNode> children;
public TreeNode(String id, String parentId) {
this.id = id;
this.parentId = parentId;
children = new ArrayList<>();
// 添加子节点
public void addChild(TreeNode node) {
children.add(node);
// 获取所有子节点
public List<TreeNode> getChildren() {
return children;
// 获取父节点
public TreeNode getParent() {
// 查询父节点
return null;
// 构建树形结构
public static TreeNode buildTree(List<TreeNode> nodeList) {
Map<String, TreeNode> map = new HashMap<>();
// 将所有节点添加到map中
for (TreeNode node : nodeList) {
map.put(node.getId(), node);
// 构建树形结构
for (TreeNode node : nodeList) {
if (!node.getParentId().equals("0")) {
map.get(node.getParentId()).addChild(node);
// 返回根节点
return map.get("0");
// 根据节点ID获取所有子节点
public static List<TreeNode> getChildNodeList(TreeNode node) {
List<TreeNode> childNodeList = new ArrayList<>();
if (node != null && !node.getChildren().isEmpty()) {
for (TreeNode childNode : node.getChildren()) {
childNodeList.addAll(getChildNodeList(childNode));
childNodeList.addAll(node.getChildren());
return childNodeList;
// 根据子节点ID获取父节点
public static TreeNode getParentNode(TreeNode rootNode, String childId) {
if (rootNode != null) {
if (rootNode.getId().equals(childId)) {
return null;
for (TreeNode childNode : rootNode.getChildren()) {
if (childNode.getId().equals(childId)) {
return rootNode;
TreeNode parentNode = getParentNode(childNode, childId);
if (parentNode != null) {
return parentNode;
return null;
// 使用示例
List<TreeNode> nodeList = new ArrayList<>();
nodeList.add(new TreeNode("0", "0"));
nodeList.add(new TreeNode("1", "0"));
nodeList.add(new TreeNode("2", "1"));
nodeList.add(new TreeNode("3", "2"));
nodeList.add(new TreeNode("4", "2"));
TreeNode rootNode = buildTree(nodeList);
List<TreeNode> childNodeList = getChildNodeList(rootNode); // 获取所有子节点
TreeNode parentNode = getParentNode(rootNode, "4"); // 获取父节点