@Override public String listCity(String parentId) throws Exception { List<City> all = cityMapper.selectProvinceInfo(); Map<String, City> cityMap = new HashMap<>(); List<City> result = new ArrayList<>(); for (City city : all){ cityMap.put(city.getCitycode(), city); } for (City city : all){ if (city.getCityparentcode().equals("0")){ result.add(city); }else { cityMap.get(city.getCityparentcode()).getChildren().add(city); } } return JSONObject.toJSONString(result); }
public class City { private String citycode; private String cityname; private String cityparentcode; private Integer citylevel; private List<City> children = new ArrayList<>(); public List<City> getChildren() { return children; } public void setChildren(List<City> children) { this.children = children; } public String getCitycode() { return citycode; } public void setCitycode(String citycode) { this.citycode = citycode == null ? null : citycode.trim(); } public String getCityname() { return cityname; } public void setCityname(String cityname) { this.cityname = cityname == null ? null : cityname.trim(); } public String getCityparentcode() { return cityparentcode; } public void setCityparentcode(String cityparentcode) { this.cityparentcode = cityparentcode == null ? null : cityparentcode.trim(); } public Integer getCitylevel() { return citylevel; } public void setCitylevel(Integer citylevel) { this.citylevel = citylevel; } }