树型结构递归查询

本文介绍了一种使用SQL进行数据库层级查询的方法,包括如何查询当前节点的所有子节点、查找根节点及根据根节点获取所有层级节点等操作。还展示了多表查询的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询当前节点的所有子字节:

@SuppressWarnings("unchecked")
public List<String> findAllChildColumnIdById(long id) throws Exception {

return em
.createNativeQuery(
"select to_char(i.id) FROM columns i start with i.id=:id connect by PRIOR i.id=i.f_parentId order by i.id asc")
.setParameter("id", id).getResultList();
}

当前节点找根节点
public String findRootIdByColumnId(long id) throws Exception {
return (String)em
.createNativeQuery(
"select to_char(i.id) FROM columns i where i.f_parentId=0 start with i.id=:id connect by PRIOR i.f_parentId=i.id ")
.setParameter("id", id).getSingleResult();
}

根据根节点找所有节点:

@SuppressWarnings("unchecked")
public List<InfoColumn> findAllChildColumnIdByRootId(long id) throws Exception {
List objects =em
.createNativeQuery(
"select i.id,i.f_name,i.f_parentId FROM columns i start with i.id=:id connect by PRIOR i.id=i.f_parentId order by i.id asc")
.setParameter("id", id).getResultList();
if(objects!=null&&objects.size()>0){

List<InfoColumn> infocs = new ArrayList<InfoColumn>();
for (Object o : objects) {
InfoColumn i = new InfoColumn();
Object[] os = (Object[]) o;
i.setId(Long.parseLong(os[0].toString()));
i.setName(os[1] != null ? os[1].toString() : "");
i.setParentId(Long.parseLong(os[2].toString()));
infocs.add(i);
}
return infocs;
}else{

return null;
}
}

多表查询:

@SuppressWarnings("unchecked")
public List<Info> findInfoByColumnIds(List<String> ids, int pageindex,
int max) throws Exception {

String QL = "select distinct o.id,o.title,o.path,o.updateDate FROM Info o,InfoColumn i where o in elements(i.infos) ";

StringBuffer temp = new StringBuffer();
if (ids != null && ids.size() > 0) {
for (String id : ids) {
temp.append(",");
temp.append(id);
}
}

List objects= em.createQuery(QL + " and i.id in("
+ temp.substring(1).toString()
+ ") and (o.state=2 or o.state=3) order by o.updateDate desc").setFirstResult(
pageindex).setMaxResults(max).getResultList();
if(objects!=null&&objects.size()>0){

List<Info> infos = new ArrayList<Info>();
for (Object o : objects) {
Info i = new Info();
Object[] os = (Object[]) o;
i.setId(Long.parseLong(os[0].toString()));
i.setTitle(os[1] != null ? os[1].toString() : "");
i.setPath(os[2] != null ? os[2].toString() : "");
infos.add(i);

}

return infos;
}else{


return null;
}
}
/** * 根据等级查询类目 * * @param level * @return */ @Override public List queryCategoryTree(Integer level) { //查询当前级别下类目 List list = categoryDAO.list(level); //组装好的类目,返回前端 List categoryTree = new ArrayList(); //所有类目 List allDTOList = new ArrayList(); if (CollectionUtils.isEmpty(list)) { return categoryTree; } for (CategoryDO categoryDO : list) { allDTOList.add(new CategoryTreeDTO().convertDOToDTO(categoryDO)); } //当前等级类目 categoryTree = allDTOList.stream().filter(dto -> level.equals(dto.getLevel())).collect(Collectors.toList()); for (CategoryTreeDTO categoryTreeDTO : categoryTree) { //组装类目为结构 assembleTree(categoryTreeDTO, allDTOList,Constants.CATEGORY_MAX_LEVEL - level); } return categoryTree; } /** * 组装 * * @param categoryTreeDTO * @param allList * @param remainRecursionCount 剩余递归次数 * @return */ public CategoryTreeDTO assembleTree(CategoryTreeDTO categoryTreeDTO, List allList, int remainRecursionCount) { remainRecursionCount--; //最大递归次数不超过Constants.CATEGORY_MAX_LEVEL-level次,防止坏数据死循环 if(remainRecursionCount < 0){ return categoryTreeDTO; } String categoryCode = categoryTreeDTO.getCategoryCode(); Integer level = categoryTreeDTO.getLevel(); //到达最后等级返回 if (Constants.CATEGORY_MAX_LEVEL == level) { return categoryTreeDTO; } //子类目 List child = allList.stream().filter(a -> categoryCode.equals(a.getParentCode())).collect(Collectors.toList()); if (null == child) { return categoryTreeDTO; } categoryTreeDTO.setChildren(child); //组装子类目 for (CategoryTreeDTO dto : child) { assembleTree(dto, allList,remainRecursionCount); } return categoryTreeDTO; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值