java 递归查询省市区树形结构

网上的具体代码实现如下:

 @RequestMapping("getTree")
    public Map<String, Object> getTree(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            ChinaCitys province = dao.findProvince(id); //查询出一个省
            if (province != null) {
                List<ChinaCitys> citys = dao.findChildren(province.getId());//查询省下面的所有市
                digui(citys); //调用递归算法查询市以下的区县
                province.setChildren(citys);
            }
            map.put("data", province);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public void digui(List<ChinaCitys> citys) {
        List<ChinaCitys> retList = new ArrayList<>();
        for (ChinaCitys c : citys) {
            retList = dao.findChildren(c.getId());
            if (retList.size() > 0) {
                c.setChildren(retList);
                digui(retList); //循环调用自己
            }
        }
    }

sql语句:

<select id="findProvince" parameterType="java.lang.Integer" resultMap="BaseResultMap">
   select id,name from china_citys
   WHERE id = #{id}
</select>

<select id="findChildren" parameterType="java.lang.Integer" resultMap="BaseResultMap">
   select id,name  from china_citys
   WHERE upid = #{id}
</select>

 

 

个人实际操作:

public List<AppExamDeptVo> getExamDeptTree() {
    List<AppExamDeptVo> appExamDeptVoList = new ArrayList<>();
    try {
        appExamDeptVoList = ntClient.appExamDeptService.selectAll(0L);
        for (AppExamDeptVo ae : appExamDeptVoList) {
            List<AppExamDeptVo> aed = ntClient.appExamDeptService.selectAll(ae.getDeptId());
            digui(aed);
            ae.setChildren(aed);
        }
    } catch (Exception e) {

    }
    return appExamDeptVoList;
}

public void digui(List<AppExamDeptVo> aed) {
    List<AppExamDeptVo> retList = new ArrayList<>();
    for (AppExamDeptVo c : aed) {
        retList = ntClient.appExamDeptService.selectAll(c.getDeptId());
        if (retList.size() > 0) {
            c.setChildren(retList);
            digui(retList); //循环调用自己
        }
    }
}

SQL语句:

<!--查询组织机构-->
<select id="selectAll" parameterType="java.lang.Long"  resultMap="AppExamDeptResult">
    <include refid="selectAppExamDeptVo"/>
    where parent_id = #{deptId}
</select>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/** * 根据等级查询类目树 * * @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、付费专栏及课程。

余额充值