@RequestMapping(value = { "/getaddressmg" })
public ModelAndView getAddressMg(HttpServletRequest request, Model model) {
ModelAndView view = this.getBaseModelAndView("custom/addressmg/addressmg");
try {
CustomInfoBean cb = this.getCurrentUser();
String customerId = cb.getCustomerId();
CusAddr cusAddr = new CusAddr();
cusAddr.setCustomerId(customerId);
// 获得用户的信息地址
List<CusAddr> cusAddrList = cusAddrService.getAddrListByNotNUll(cusAddr);
if (null != cusAddrList) {
for (CusAddr cus : cusAddrList) {
//获取省的信息
String provinceId = cus.getProvinceId();
if (StringUtils.isNotBlank(provinceId)) {
Region regionProvince = new Region();
if((Constant.REGION_SOURCE_JD).equalsIgnoreCase(cus.getSource())){
regionProvince = regionService.getRegionBySourceregid(cus.getProvinceId(), Constant.REGION_SOURCE_JD);
}else if((Constant.REGION_SOURCE_STB).equalsIgnoreCase(cus.getSource())){
regionProvince = regionService.getRegionBySourceregid(cus.getProvinceId(), Constant.REGION_SOURCE_STB);
}else{
regionProvince = regionService.getRegionById(cus.getProvinceId());
}
if(null == regionProvince){
continue;
};
cus.setProvinceName(regionProvince.getRegionName());
}
//获取市的信息
String cityId = cus.getCityId();
if (StringUtils.isNotBlank(cityId)) {
Region regionCity = regionService.getRegionById(cityId);
//Region regionCity = new Region();
if((Constant.REGION_SOURCE_JD).equalsIgnoreCase(cus.getSource())){
regionCity = regionService.getRegionBySourceregid(cus.getCityId(), Constant.REGION_SOURCE_JD);
}else if((Constant.REGION_SOURCE_STB).equalsIgnoreCase(cus.getSource())){
regionCity = regionService.getRegionBySourceregid(cus.getCityId(), Constant.REGION_SOURCE_STB);
}else{
regionCity = regionService.getRegionById(cus.getCityId());
}
if(null == regionCity){
continue;
}
cus.setCityName(regionCity.getRegionName());
}
//获取区的信息
String districtId = cus.getDistrictId();
if (StringUtils.isNotBlank(districtId)) {
Region regionDistrict = regionService.getRegionById(districtId);
//Region regionDistrict = new Region();
if((Constant.REGION_SOURCE_JD).equalsIgnoreCase(cus.getSource())){
regionDistrict = regionService.getRegionBySourceregid(cus.getDistrictId(), Constant.REGION_SOURCE_JD);
}else if((Constant.REGION_SOURCE_STB).equalsIgnoreCase(cus.getSource())){
regionDistrict = regionService.getRegionBySourceregid(cus.getDistrictId(), Constant.REGION_SOURCE_STB);
}else{
regionDistrict = regionService.getRegionById(cus.getDistrictId());
}
if(null == regionDistrict){
continue;
}
cus.setDistrictName(regionDistrict.getRegionName());
}
//获取县的信息
String tradId = cus.getTradId();
if (StringUtils.isNotBlank(tradId)) {
Region regionTrading = regionService.getRegionById(tradId);
if((Constant.REGION_SOURCE_JD).equalsIgnoreCase(cus.getSource())){
regionTrading = regionService.getRegionBySourceregid(cus.getTradId(), Constant.REGION_SOURCE_JD);
}else if((Constant.REGION_SOURCE_STB).equalsIgnoreCase(cus.getSource())){
regionTrading = regionService.getRegionBySourceregid(cus.getTradId(), Constant.REGION_SOURCE_STB);
}else{
regionTrading = regionService.getRegionById(cus.getTradId());
}
if(null == regionTrading){
continue;
}
cus.setTradingName(regionTrading.getRegionName());
}
}
}
//获取商家信息
view.addObject("cuslist", cusAddrList);
} catch (BusinessException be) {
logger.error("查看地址失败", be);
this.forwordErrorPage(model, be.getMessage());
} catch (Exception ex) {
logger.error("查看地址失败", ex);
this.forwordErrorPage(model, ExceptionMsg.getSystemErrorMsg());
}
return view;
}
领导说for循环中尽量不要请求数据库,应该怎么改一下
说一下自己的看法:
1.控制层还是尽量不要写逻辑,应该一到Service层去;
2.你这个for循环里面的查询数据库是每次只查一条,查询多次,那为什么不用一条查询语句全部查出来呢。
3.还有就是可以用冗余字段的方法来尽量减少查询数据库的次数,比如你可以把那些个地址中经常需要查询的,直接加到user对应的表里面去,缺点是需要更改表结构,还需要用到mq。。。