说明:本文中只是一个记录,后台返回递归数据
tree实体:
import com.baomidou.mybatisplus.annotation.TableField; import lombok.Getter; import lombok.Setter; import java.util.List; @Setter @Getter public class SfEntityList { private String entityListId; private String entityChineseName; private String entityEnglishName; private String entityFrenchName; private String entityNodeType; private String parentId; @TableField(exist = false) private List<SfEntityList> children;//每个树节点下的子节点 }
递归方法:
public List<SfEntityList> findShowTree(){ Map<String,Object> rootRoleMap = new HashMap<String,Object>(); rootRoleMap.put("entityNodeType",TreeEnum.TreeNodeType.TREEROOT.getIndex());//一级节点类型 List<SfEntityList> list = sfEntityListMapper.findRoleSfEntityListForSys(rootRoleMap);//查询所有的一级节点 Map<String,Object> allRoleMap = new HashMap<String,Object>(); List<SfEntityList> allRoleList = sfEntityListMapper.findRoleSfEntityListForSys(allRoleMap);//查询全部节点 for(SfEntityList entityList : list){ entityList.setChildren(findChildren(entityList,allRoleList)); } return list; } public List<SfEntityList> findChildren(SfEntityList entityList,List<SfEntityList> allRoleList){ List<SfEntityList> list = new ArrayList<SfEntityList>(); for(SfEntityList r : allRoleList){ if(entityList.getEntityListId().equals(r.getParentId())){ list.add(r); } } for(SfEntityList r : list){ r.setChildren(findChildren(r,allRoleList)); } return list; }
总结:一个简单的递归