Javabean和map相互轉(zhuǎn)化方法代碼示例
在做導(dǎo)入的時(shí)候,遇到了需要將map對(duì)象轉(zhuǎn)化 成javabean的問題,也就是說,不清楚javabean的內(nèi)部字段排列,只知道m(xù)ap的 key代表javabean的字段名,value代表值。
那現(xiàn)在就需要用轉(zhuǎn)化工具了。是通用的哦!
首先來看 JavaBean 轉(zhuǎn)化成Map的方法:
/**
* 將一個(gè) JavaBean 對(duì)象轉(zhuǎn)化為一個(gè) Map
* @param bean 要轉(zhuǎn)化的JavaBean 對(duì)象
* @return 轉(zhuǎn)化出來的 Map 對(duì)象
* @throws IntrospectionException 如果分析類屬性失敗
* @throws IllegalAccessException 如果實(shí)例化 JavaBean 失敗
* @throws InvocationTargetException 如果調(diào)用屬性的 setter 方法失敗
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map convertBean(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
下面是將Map轉(zhuǎn)化成JavaBean對(duì)象的方法:
/**
* 將一個(gè) Map 對(duì)象轉(zhuǎn)化為一個(gè) JavaBean
* @param type 要轉(zhuǎn)化的類型
* @param map 包含屬性值的 map
* @return 轉(zhuǎn)化出來的 JavaBean 對(duì)象
* @throws IntrospectionException 如果分析類屬性失敗
* @throws IllegalAccessException 如果實(shí)例化 JavaBean 失敗
* @throws InstantiationException 如果實(shí)例化 JavaBean 失敗
* @throws InvocationTargetException 如果調(diào)用屬性的 setter 方法失敗
*/
@SuppressWarnings("rawtypes")
public static Object convertMap(Class type, Map map)
throws IntrospectionException, IllegalAccessException,
InstantiationException, InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 獲取類屬性
Object obj = type.newInstance(); // 創(chuàng)建 JavaBean 對(duì)象
// 給 JavaBean 對(duì)象的屬性賦值
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
// 下面一句可以 try 起來,這樣當(dāng)一個(gè)屬性賦值失敗的時(shí)候就不會(huì)影響其他屬性賦值。
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
descriptor.getWriteMethod().invoke(obj, args);
}
}
return obj;
}
以上內(nèi)容我測試過,是沒有問題的,供大家參考學(xué)習(xí)。感謝大家對(duì)本站的支持。
相關(guān)文章
JAVA下單接口優(yōu)化實(shí)戰(zhàn)TPS性能提高10倍
今天小編就為大家分享一篇關(guān)于JAVA下單接口優(yōu)化實(shí)戰(zhàn)TPS性能提高10倍,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
@RequestAttribute和@RequestParam注解的區(qū)別及說明
這篇文章主要介紹了@RequestAttribute和@RequestParam注解的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
FP-Growth算法的Java實(shí)現(xiàn)+具體實(shí)現(xiàn)思路+代碼
FP-Growth算法比Apriori算法快很多(但是卻比不上時(shí)間,how time slipped away)。在網(wǎng)上搜索后發(fā)現(xiàn)Java實(shí)現(xiàn)的FP-Growth算法很少,且大多數(shù)不太能理解):太菜。所以就自己實(shí)現(xiàn)了一下。這篇文章重點(diǎn)介紹一下我的Java實(shí)現(xiàn)2021-06-06
Spring Bean Scope 有狀態(tài)的Bean與無狀態(tài)的Bean
這篇文章主要介紹了Spring Bean Scope 有狀態(tài)的Bean與無狀態(tài)的Bean,每個(gè)用戶有自己特有的一個(gè)實(shí)例,在用戶的生存期內(nèi),bean保持了用戶的信息,下面來了解有狀態(tài)和無狀態(tài)的區(qū)別吧2022-01-01
IDEA打包maven多模塊相互依賴項(xiàng)目全過程
這篇文章主要介紹了IDEA打包maven多模塊相互依賴項(xiàng)目全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

