JavaBean和Map轉換封裝類的方法
更新時間:2016年10月12日 09:40:40 投稿:jingxian
下面小編就為大家?guī)硪黄狫avaBean和Map轉換封裝類的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
package com.ljq.util; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Map工具類 * * @author jqlin */ public class MapUtils { /** * 從map集合中獲取屬性值 * * @param <E> * @param map * map集合 * @param key * 鍵對 * @param defaultValue * 默認值 * @return * @author jiqinlin */ @SuppressWarnings({ "unchecked", "rawtypes" }) public final static <E> E get(Map map, Object key, E defaultValue) { Object o = map.get(key); if (o == null) return defaultValue; return (E) o; } /** * Map集合對象轉化成 JavaBean集合對象 * * @param javaBean JavaBean實例對象 * @param mapList Map數(shù)據(jù)集對象 * @return * @author jqlin */ @SuppressWarnings({ "rawtypes" }) public static <T> List<T> map2Java(T javaBean, List<Map> mapList) { if(mapList == null || mapList.isEmpty()){ return null; } List<T> objectList = new ArrayList<T>(); T object = null; for(Map map : mapList){ if(map != null){ object = map2Java(javaBean, map); objectList.add(object); } } return objectList; } /** * Map對象轉化成 JavaBean對象 * * @param javaBean JavaBean實例對象 * @param map Map對象 * @return * @author jqlin */ @SuppressWarnings({ "rawtypes","unchecked", "hiding" }) public static <T> T map2Java(T javaBean, Map map) { try { // 獲取javaBean屬性 BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass()); // 創(chuàng)建 JavaBean 對象 Object obj = javaBean.getClass().newInstance(); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if (propertyDescriptors != null && propertyDescriptors.length > 0) { String propertyName = null; // javaBean屬性名 Object propertyValue = null; // javaBean屬性值 for (PropertyDescriptor pd : propertyDescriptors) { propertyName = pd.getName(); if (map.containsKey(propertyName)) { propertyValue = map.get(propertyName); pd.getWriteMethod().invoke(obj, new Object[] { propertyValue }); } } return (T) obj; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * JavaBean對象轉化成Map對象 * * @param javaBean * @return * @author jqlin */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map java2Map(Object javaBean) { Map map = new HashMap(); try { // 獲取javaBean屬性 BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if (propertyDescriptors != null && propertyDescriptors.length > 0) { String propertyName = null; // javaBean屬性名 Object propertyValue = null; // javaBean屬性值 for (PropertyDescriptor pd : propertyDescriptors) { propertyName = pd.getName(); if (!propertyName.equals("class")) { Method readMethod = pd.getReadMethod(); propertyValue = readMethod.invoke(javaBean, new Object[0]); map.put(propertyName, propertyValue); } } } } catch (Exception e) { e.printStackTrace(); } return map; } }
以上就是小編為大家?guī)淼腏avaBean和Map轉換封裝類的方法全部內容了,希望大家多多支持腳本之家~
相關文章
使用SpringBoot項目導入openfeign版本的問題
這篇文章主要介紹了使用SpringBoot項目導入openfeign版本的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java并發(fā)系列之CountDownLatch源碼分析
這篇文章主要為大家詳細介紹了Java并發(fā)系列之CountDownLatch源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03Java基于API接口爬取商品數(shù)據(jù)的示例代碼
Java作為一種流行的編程語言,可以用于編寫程序來調用這些API接口,從而獲取商品數(shù)據(jù),本文將介紹如何使用Java基于API接口爬取商品數(shù)據(jù),包括請求API、解析JSON數(shù)據(jù)、存儲數(shù)據(jù)等步驟,并提供相應的代碼示例,感興趣的朋友跟隨小編一起看看吧2023-10-10springboot攔截器不攔截靜態(tài)資源,只攔截controller的實現(xiàn)方法
這篇文章主要介紹了springboot攔截器不攔截靜態(tài)資源,只攔截controller的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07