zoukankan      html  css  js  c++  java
  • JavaBean和Map转换封装类

    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数据集对象
         * @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());
                // 创建 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;
        }
     
    }
  • 相关阅读:
    SAP MM 采购附加费计入物料成本之二
    SAP MM 采购附加费计入物料成本?
    SAP MM 作为采购附加费的运费为啥没能在收货的时候计入物料成本?
    SAP MM 外部采购流程里的Advanced Return Management
    SAP MM 外部采购流程里的如同鸡肋一样的Advanced Returns Management功能
    SAP MM Why is the freight not included in the material cost at the time of GR?
    SAP MM: Change of material moving average price after goods receipt and invoice verification posting for PO
    SAP 创建启用了ARM功能的采购订单,报错 Shipping processing is not selected to supplier 100057 in purchase org. 0002
    GIT·代码仓库默认分支更改
    .Net/C#·运行报错缺少XXX文件,但双击无法跳转缺少位置
  • 原文地址:https://www.cnblogs.com/linjiqin/p/5546089.html
Copyright © 2011-2022 走看看