import java.util.Map;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;

/***
 * JavaBean对象与Map对象互相转化
 */
public class ConverMapToObject {

    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null){
            return null;
        }
        Object obj = beanClass.newInstance();
        BeanUtils.populate(obj, map);
        return obj;
    }

    public static Map<?, ?> objectToMap(Object obj) {
        if (obj == null){
            return null;
        }
        return new BeanMap(obj);
    }
}

 

posted on 2018-11-12 16:27  沛昕的博客  阅读(508)  评论(0编辑  收藏  举报