一個遍歷MAP的方法

遍歷map,然後根據map中的key值來取對應的value,來一段實際中用的一種方法吧:

 public String edit(){
       try {
        Map map = WebUtils.getParameterMap();
         PersonBO pb = new PersonBO(); 
         for (Iterator i = map.keySet().iterator(); i.hasNext();) {  
               Object obj = i.next();  
              if (obj.equals("id")) {
                  Long id = Long.valueOf((String) map.get(obj));
                  pb.setId(id);
            }
              if (obj.equals("userName")) {
                    pb.setUserName((String)map.get(obj));
                }
              if (obj.equals("trueName")) {
                pb.setTrueName((String)map.get(obj));
            }
              if (obj.equals("mobilePhone")) {
                    pb.setMobilePhone((String)map.get(obj));
                }
              if (obj.equals("idCard")) {
                    pb.setIdCard((String)map.get(obj));
                }
              if (obj.equals("age")) {
                  Integer age = Integer.valueOf((String) map.get(obj));
                    pb.setAge(age);
                }
              if (obj.equals("isDelete")) {
                  Integer isDelete = Integer.valueOf((String) map.get(obj));
                    pb.setIsDelete(isDelete);
                }
              if (obj.equals("userId")) {
                  Long userId = Long.valueOf((String) map.get(obj));
                    pb.setUserId(userId);
              }
              if (obj.equals("headPhoto")) {
                    pb.setHeadPhoto((String) map.get(obj));
                }
              if (obj.equals("headPhotoDatas")) {
                  pb.setHeadPhotoDatas((String)map.get(obj));
              }
          
              }  
        
        personService.updatePersonDynamic(pb.getId(),pb.getUserName(),pb.getTrueName(),pb.getMobilePhone(),
                pb.getIdCard(),pb.getAge(),pb.getIsDelete());
        
         UserAccountBO accountBO = userManageService.queryAccountById(pb.getUserId());
            if (accountBO == null) {
                msg = "此賬號id不存在";
                return getResult();
            }

            long imgSize = Base64Util.getImgSize(pb.getHeadPhotoDatas());
            if (imgSize > 2 * 1024 * 1024){
                msg = "圖片不能大於2M";
                return getResult();
            }

            // 頭像存放地址前綴
            String filePath = PropKit.use("properties/url.properties").get("headPhoto.path");
            String fileName = NumberUtil.getCustomNum(16) + ".jpg";
            Base64Util.generateImage(pb.getHeadPhotoDatas(), filePath + fileName);

            // 更新頭像
            String headPhotoPrefix = PropKit.use("properties/url.properties").get("headPhoto.prefix");
            UserAccountBO userAccountBO = new UserAccountBO();
            userAccountBO.setId(pb.getUserId());
          //判斷是否是新上傳的頭像,是-就更新URL,否-就保存原來的URL
            String headPhoto;
            if (pb.getHeadPhoto().length()>0) {
                  headPhoto = pb.getHeadPhoto().substring(pb.getHeadPhoto().length()-31,pb.getHeadPhoto().length());
            } else {
                 headPhoto=pb.getHeadPhoto();
            }
           
            if (pb.getHeadPhotoDatas() != null && pb.getHeadPhotoDatas().length() != 0) {                
                userAccountBO.setHeadPhoto(headPhotoPrefix + fileName);
            } else {
                
                userAccountBO.setHeadPhoto(headPhoto);
            }
            userManageService.update(userAccountBO);

            // TODO 回顯待測試通過
            String showUrl = PropKit.use("properties/url.properties").get("showImage.path.prefix")
                    + headPhotoPrefix + fileName;
        
        msg = "修改成功";
    } catch (Exception e) {
        this.code = -1;
        this.msg = e.getMessage();
        this.expMsg = getExceptionMessage(e);
        log.error(e.getMessage());
    }
    return getResult();
       
   }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章