先查询第一层的数据,然后调用递归循环第一层的数据,查询父Id等于第一层的Id,执行完成后第一层一下的所有数据就全部查询出来了。。。
public List<Information> getTreeList(Integer topId) {
String hql="from Information where isDelete=2 and id="+topId;
List<Information> entityList=baseDao.queryHQL(hql);
entityList.addAll(getSubList(entityList));
return entityList;
}
private List<Information> getSubList(List<Information> entityList) {
for (int i = 0; i < entityList.size(); i++) {
String hql="from Information where isDelete=2 and fatherId="+entityList.get(i).getId();
if(baseDao.queryHQL(hql).size()>0){
List<Information> list2=baseDao.queryHQL(hql);
entityList.get(i).setList(list2);
getSubList(entityList.get(i).getList());
}
}
return entityList;
}
本文介绍了一种使用递归方法查询树形结构数据的方法。首先查询顶级节点,然后递归地查询每个子节点,直到获取整个树形结构的所有数据。
4858

被折叠的 条评论
为什么被折叠?



