java遞歸查詢省市區樹形結構

具體代碼實現以下: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>
相關文章
相關標籤/搜索