对象转换工具类BeanUtil

public class BeanUtil {
    private static final Logger logger = LoggerFactory.getLogger(BeanUtil.class);

    /**
	 * 功能描述:浅拷贝 - 转换Bean对象
	 * 
	 * @param sourceObject
	 * @param clazz
	 * @return
	 * @author Elivense White
	 */
	public static  T convertToBean(Object sourceObject, Class clazz) {
		T result = null;
		if (sourceObject != null) {
			try {
				result = clazz.newInstance();
			} catch (Exception e) {
				logger.error("convert error", e);
				e.printStackTrace();
			}
			BeanUtils.copyProperties(sourceObject, result);
		}
		return result;
	}


   /**
	 * 功能描述:浅拷贝 - 转换List 对象
	 * 
	 * @param sourceList
	 * @param clazz
	 * @return
	 * @author Elivense White
	 */
	public static  List convertToBeanList(List sourceList, Class clazz) {
		List resultList = null;
		try {
			if (sourceList != null && sourceList.size() > 0) {
				resultList = new ArrayList();

				Iterator iterator = sourceList.iterator();
				while (iterator.hasNext()) {
					T t = clazz.newInstance();
					Object sourceObject = iterator.next();
					BeanUtils.copyProperties(sourceObject, t);
					resultList.add(t);
				}
			}
		} catch (Exception e) {
			logger.error("convert error", e);
			e.printStackTrace();
		}
		return resultList;
	}

 /**
     * 将对象装换为map
     *
     * @param bean
     * @return
     */
    public static  Map beanToMap(T bean) {
        Map map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        return map;
    }

 /**
     * 将map装换为javabean对象
     *
     * @param map
     * @param bean
     * @return
     */
    public static  T mapToBean(Map map, T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }

 /**
     * 将List转换为List>
     *
     * @param objList
     * @return
     * @throws JsonGenerationException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static  List> objectsToMaps(List objList) {
        List> list = Lists.newArrayList();
        if (objList != null && objList.size() > 0) {
            Map map = null;
            T bean = null;
            for (int i = 0, size = objList.size(); i < size; i++) {
                bean = objList.get(i);
                map = beanToMap(bean);
                list.add(map);
            }
        }
        return list;
    }

/**
     * 将List>转换为List
     *
     * @param maps
     * @param clazz
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static  List mapsToObjects(List> maps, Class clazz)
            throws InstantiationException, IllegalAccessException {
        List list = Lists.newArrayList();
        if (maps != null && maps.size() > 0) {
            Map map = null;
            T bean = null;
            for (int i = 0, size = maps.size(); i < size; i++) {
                map = maps.get(i);
                bean = clazz.newInstance();
                mapToBean(map, bean);
                list.add(bean);
            }
        }
        return list;
    }


}

 

你可能感兴趣的:(工具类)