具体代码实现如下:
1@RequestMapping("getTree") 2 public Map<String, Object> getTree(int id) { 3 Map<String, Object> map = new HashMap<>(); 4 try { 5 ChinaCitys province = dao.findProvince(id); //查询出一个省 6 if (province != null) { 7 List<ChinaCitys> citys = dao.findChildren(province.getId());//查询省下面的所有市 8 digui(citys); //调用递归算法查询市以下的区县 9 province.setChildren(citys); 10 } 11 map.put("data", province); 12 } catch (Exception e) { 13 e.printStackTrace(); 14 } 15 return map; 16 } 17 18 public void digui(List<ChinaCitys> citys) { 19 List<ChinaCitys> retList = new ArrayList<>(); 20 for (ChinaCitys c : citys) { 21 retList = dao.findChildren(c.getId()); 22 if (retList.size() > 0) { 23 c.setChildren(retList); 24 digui(retList); //循环调用自己 25 } 26 } 27 }
sql语句:
1<select id="findProvince" parameterType="java.lang.Integer" resultMap="BaseResultMap"> 2 select id,name from china_citys 3 WHERE id = #{id} 4 </select> 5 6 <select id="findChildren" parameterType="java.lang.Integer" resultMap="BaseResultMap"> 7 select id,name from china_citys 8 WHERE upid = #{id} 9 </select>