斷點(diǎn)

          每天進(jìn)步一點(diǎn)點(diǎn)!
          posts - 174, comments - 56, trackbacks - 0, articles - 21

          PropertyUtils

          Posted on 2010-05-30 09:51 斷點(diǎn) 閱讀(552) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Apache
          實(shí)體bean。
          package com.ztf;

          public class Entity {
              
          private Integer id;
              
          private String name;
              
              
          public void sayHello(){
                  System.out.println(
          "sayHello()---> 無(wú)參");
              }

              
              
          public void sayHello(String s){
                  System.out.println(
          "sayHello()---> 有1個(gè)參數(shù)" );
              }

              
              
          public void sayHello(Integer a,Integer b){
                  System.out.println(
          "sayHello()---> 有2個(gè)參數(shù)");
              }

            
              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public Integer getId() {
                  
          return id;
              }

              
          public void setId(Integer id) {
                  
          this.id = id;
              }

          }


          package com.ztf;
          import java.util.Map;

          import org.apache.commons.beanutils.MethodUtils;
          import org.apache.commons.beanutils.PropertyUtils;

          public class TestPropertyUtils {  
                  
          public static void main(String[] args) throws Exception{  
                        
                      Entity entity 
          = new Entity();  
                      entity.setId(
          1) ;
                      entity.setName(
          "斷點(diǎn)");
                      
                      
          // 通過(guò)PropertyUtils的getProperty方法獲取指定屬性的值
                      Integer id = (Integer)PropertyUtils.getProperty(entity, "id");  
                      String name 
          = (String)PropertyUtils.getProperty(entity, "name");  
                      System.out.println(
          "id = " + id + "  name = " + name);  
                        
                      
          // 調(diào)用PropertyUtils的setProperty方法設(shè)置entity的指定屬性
                      PropertyUtils.setProperty(entity, "name""每天進(jìn)步一點(diǎn)");  
                      System.out.println(
          "name = " + entity.getName());  
                        
                      
          // 通過(guò)PropertyUtils的describe方法把entity的所有屬性與屬性值封裝進(jìn)Map中
                      Map map = PropertyUtils.describe(entity);  
                      System.out.println(
          "id = " + map.get("id"+ "  name = " + map.get("name"));  
                        
                            }
            
          }
            

          輸出:
          id = 1  name = 斷點(diǎn)
          name = 每天進(jìn)步一點(diǎn)
          id = 1  name = 每天進(jìn)步一點(diǎn)


          其它例子:
          import org.apache.commons.beanutils.PropertyUtils;
          List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
          for(BaseGrpMemberVO member : MemberVO){
                    Map m = new HashMap();
                    for(Map.Entry<String, String> entry: fieldMap.entrySet()){
                              String key = entry.getKey();
                              fieldList.add(key); //記錄字段名
                              String[] keyArray = key.split("\\.");
                              if(keyArray.length == 2){//成員信息
                                  Object o =PropertyUtils.getProperty(member, keyArray[1]);
                                  m.put(key, o==null?"":o.toString());
                              }else if(keyArray.length == 3){//險(xiǎn)別信息
                                  List<BaseGrpCvrgVO> cvrgVoList = mgr.getRelCvrgById(member.getCPkId());
                                  String cvrgNo = keyArray[0];
                                  for(BaseGrpCvrgVO cvrgVo : cvrgVoList){
                                      if(cvrgNo.equals(cvrgVo.getCCvrgNo())){
                                          Object o =PropertyUtils.getProperty(cvrgVo, keyArray[2]);
                                          m.put(key, o==null?"":o.toString());
                                      }
                                  }   
                              }
                          }
                    data.add(m);
                 }

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 时尚| 芮城县| 法库县| 永清县| 镇原县| 西城区| 罗源县| 武城县| 新泰市| 鄄城县| 含山县| 梨树县| 会宁县| 宁南县| 安图县| 吕梁市| 宾阳县| 兴城市| 海丰县| 葫芦岛市| 正宁县| 神池县| 桑日县| 乌拉特前旗| 宜宾县| 垣曲县| 彰武县| 大埔县| 长阳| 赣榆县| 石城县| 独山县| 广安市| 南木林县| 舟曲县| 钦州市| 吉水县| 承德县| 娄烦县| 凯里市| 杭州市|