1. 程式人生 > >04-體驗一下apache組織封裝的BeanUtil工具包

04-體驗一下apache組織封裝的BeanUtil工具包

ash spa strong shm ron string commons 我們 except

apache 自己為程序員們封裝了一個專門用於處理的工具類,其功能有(數據類型會自動轉成與JavaBean相關的)

map轉javabean

javabean轉map

javabean對象復制

獲取javabean對象屬性值

設置javabean對象屬性值…………

兩個相關jar包文件 Build Path到項目當中去

commons-beanutils-1.9.2.jar

commons-logging-1.2.jar

技術分享圖片

1.將Map轉換成JavaBean對象

/**
     * 劉詩華
     * @param args
     * @throws Exception 
     
*/ public static void main(String[] args) throws Exception { Map<String, Object> m=new HashMap<String, Object>(); m.put("id", "28"); m.put("userName", "劉詩華"); m.put("password", "123456"); User user=new User();
//BeanUtils.copyProperties(dest, orig);  dest:目標 orig:源 BeanUtils.copyProperties(user,m); System.out.println(user); //結果:User(id=28, userName=劉詩華, password=123456) Integer id = user.getId(); //我們設置給Map集合的時候,給的是一個字符串,BeanUtils工具自動幫我們轉換成包裝類Integer類型 System.out.println(id); }

04-體驗一下apache組織封裝的BeanUtil工具包