最近在工作中被map折腾快疯了,工作的同时特别喜欢用HashMap老返回数据,灵活性特别不好。
一、map的基本应用
1、map的定义
Map<String, String> parameters = new HashMap<>();
parameters.put("appKey", "k");
parameters.put("tableName", "order");
parameters.put("primaryKey", orderId);
2、获取map中的值或对象
Object = map.get("detail");
根据不同类型的数据,获取不同的数据,如果返回的是一个object,需要使用object来接。
JSONObject dataObject = jsonObject.getJSONObject("data");
Integer errCode = dataObject.getInt("errCode");
3、用for解析List List<Map<String, Object>>
for (Map<String, Object> country : countryList)
provinceList = settingService.getProvinceInfo(String.valueOf(country.get("DISTRICT_ID")));
country.put("provinceList", provinceList);
二、使用map将(国家-省-区)进行重新封装传给vue
1、获取国家省区的接口代码
@ResponseBody
@GetMapping("/getSelectAddressInfo")
public BaseResponse getSelectAddressInfo(HttpServletRequest request) {
BaseResponse response = BaseResponse.success();
try {
String provinceId = request.getParameter("provinceId");
String cityId = request.getParameter("cityId");
List<Map<String, Object>> countryList = settingService.getCountyInfo();
List<Map<String, Object>> provinceList = null;
List<Map<String, Object>> cityList = null;
for (Map<String, Object> country : countryList)
{
if(StringUtils.isNotEmpty(provinceId)){
provinceList = settingService.getProvinceInfo(provinceId);
country.put("provinceList", provinceList);
for (Map<String, Object> province : provinceList){
if(StringUtils.isNotEmpty(cityId)){
cityList = settingService.getCityInfo(cityId);
province.put("cityList",cityList);
break;
}else{
cityList = settingService.getCityInfo(String.valueOf(province.get("DISTRICT_ID")));
province.put("cityList",cityList);
}
}
break;
}else{
provinceList = settingService.getProvinceInfo(String.valueOf(country.get("DISTRICT_ID")));
country.put("provinceList", provinceList);
for (Map<String, Object> province : provinceList){
if(StringUtils.isNotEmpty(cityId)){
cityList = settingService.getCityInfo(cityId);
province.put("cityList",cityList);
break;
}else{
cityList = settingService.getCityInfo(String.valueOf(province.get("DISTRICT_ID")));
province.put("cityList",cityList);
}
}
}
}
response.setExtData(ImmutableMap.builder().put("countryList", convertFirstDto(countryList,0)).put("showMsg", "国家信息获取成功").build());
} catch (Exception e) {
log.error("getProvinceInfo error", e);
response.setExtData(ImmutableMap.builder().put("showMsg", "国家信息获取失败").build());
}
return response;
}
2、多加一层将DISTRICT_ID和DISTRICT_NAME转为value和label
private List<AddressTreeDto> convertFirstDto(List<Map<String, Object>> list, int level){
if(list==null){
return null;
}
level++;
List<AddressTreeDto> dtos=new ArrayList<>();
for(Map<String, Object> tree:list){
AddressTreeDto dto=new AddressTreeDto();
dto.setCode(String.valueOf(tree.get("PHONE_PREFIX")));
dto.setValue(String.valueOf(tree.get("DISTRICT_ID")));
dto.setLabel(String.valueOf(tree.get("DISTRICT_NAME")));
dto.setLevel(level);
List<Map<String, Object>> resultList = null;
resultList = (List)tree.get("provinceList");
if(CollectionUtils.isNotEmpty(resultList)){
dto.setChildren(convertDto(resultList,level));
}
dtos.add(dto);
}
return dtos;
}
private List<AddressTreeDto> convertDto(List<Map<String, Object>> list, int level){
if(list==null){
return null;
}
level++;
List<AddressTreeDto> cityDtos=new ArrayList<>();
for(Map<String, Object> cityTree:list){
AddressTreeDto citydto=new AddressTreeDto();
citydto.setValue(String.valueOf(cityTree.get("DISTRICT_ID")));
citydto.setLabel(String.valueOf(cityTree.get("DISTRICT_NAME")));
citydto.setLevel(level);
List<Map<String, Object>> resultList = (List)cityTree.get("cityList");
if(CollectionUtils.isNotEmpty(resultList)){
citydto.setChildren(convertDto(resultList,level));
}
cityDtos.add(citydto);
}
return cityDtos;
}