在工程中加入工具類:commons-beanutils-1.7.0.jarjava
下載地址:http://mvnrepository.com/apache
或者在pom.xml中增長如下:
工具
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>code
或者:xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
對象
package com.taopl.test; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; /** * <p>描 述:對象與map互轉工具類</p> * <p>創 建 人:test</p> * <p>建立時間:Mar 4, 2015 3:16:39 PM</p> */ public class JBeanUtils { /** * 將一個 Map 對象轉化爲一個 JavaBean * * @param type * 要轉化的類型 * @param map * 包含屬性值的 map * @return 轉化出來的 JavaBean 對象 * @throws IntrospectionException * 若是分析類屬性失敗 * @throws IllegalAccessException * 若是實例化 JavaBean 失敗 * @throws InstantiationException * 若是實例化 JavaBean 失敗 * @throws InvocationTargetException * 若是調用屬性的 setter 方法失敗 */ @SuppressWarnings("unchecked") public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { Object obj = type.newInstance(); // 建立 JavaBean 對象 BeanUtils.populate(obj, map); /** * 將null string轉爲空串 * @date 2014-12-22 * */ // ConvertUtilsBean convertUtils = new ConvertUtilsBean(); // convertUtils.register(new Converter() { // public Object convert(Class type, Object value) { // if (value == null) // return ""; // return value; // } // }, String.class); // BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils, // new PropertyUtilsBean()); // beanUtils.populate(obj, map); return obj; } /** * 將一個 JavaBean 對象轉化爲一個 Map * * @param bean * 要轉化的JavaBean 對象 * @return 轉化出來的 Map 對象 * @throws IntrospectionException * 若是分析類屬性失敗 * @throws IllegalAccessException * 若是實例化 JavaBean 失敗 * @throws InvocationTargetException * 若是調用屬性的 setter 方法失敗 */ @SuppressWarnings("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 * * @describe:TODO * @param beans * @return * @throws IllegalAccessException * @throws InvocationTargetException * @throws IntrospectionException * @time:2014年9月3日上午10:37:32 */ @SuppressWarnings("unchecked") public static List<Map> convertBeans(List<Object> beans) throws IllegalAccessException, InvocationTargetException, IntrospectionException { List<Map> maps = new ArrayList<Map>(); for (Iterator iterator = beans.iterator(); iterator.hasNext();) { Object bean = (Object) iterator.next(); maps.add(convertBean(bean)); } return maps; } /** * 將對多個Map轉對對象集合返回 * * @describe:TODO * @param type * @param maps * @return * @throws IntrospectionException * @throws IllegalAccessException * @throws InstantiationException * @throws InvocationTargetException * @time:2014年9月3日上午10:40:00 */ @SuppressWarnings("unchecked") public static List<Object> convertMaps(Class type, List<Map> maps) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { List<Object> beans = new ArrayList<Object>(); for (Map map : maps) { beans.add(convertMap(type, map)); } return beans; } /** * 對象複製 * * @describe:TODO * @param toBean * 目標對象 * @param fromBean * 對象來源 * @return * @throws IllegalAccessException * @throws InvocationTargetException * @time:2014年9月3日上午11:47:45 */ public static Object copyProperties(Object toBean, Object fromBean) throws IllegalAccessException, InvocationTargetException { if (fromBean == null) { return null; } BeanUtils.copyProperties(toBean, fromBean); return toBean; } /** * 對象複製(將給定的對象轉化爲給定的Class 類型對象並返回) * * @describe:TODO * @param toClassBean * @param fromBean * @return * @throws IllegalAccessException * @throws InvocationTargetException * @throws InstantiationException * @throws ClassNotFoundException * @time:2014年9月3日下午12:05:23 */ public static Object copyProperties(Class toClassBean, Object fromBean) throws IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException { if (fromBean == null) { return null; } Object toBean = Class.forName(toClassBean.getCanonicalName()) .newInstance(); return copyProperties(toBean, fromBean); } /** * 將給定的對象集合轉換爲指定的類對象集合 * * @describe:TODO * @param toClassBean * 類 類型 * @param beans * 對象集合 * @return * @throws IllegalAccessException * @throws InvocationTargetException * @throws InstantiationException * @throws ClassNotFoundException * @time:2014年9月3日下午12:33:24 */ public static List copyProperties(Class toClassBean, List beans) throws IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException { List list = new ArrayList(); for (Iterator iterator = beans.iterator(); iterator.hasNext();) { Object object = (Object) iterator.next(); list.add(copyProperties(toClassBean, object)); } return list; } public static void main(String[] args) throws Exception { //User user = new User(); // System.out.println(convertBean(user)); //GpGroup gp = (GpGroup) copyProperties(GpGroup.class, user); // System.out.println(gp); } }