zoukankan      html  css  js  c++  java
  • map填充bean赋值,包括父类全部填充。

    有不少工具类给bean填充值。但是填充,很多都是只能填充到当前类的对象。经过需求修改,做了个工具类:

    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import com.iafclub.baseTools.vo.PageResultVo;
    import com.iafclub.baseTools.vo.PageVo;
    /**对象填充类
     * 
     * @author 陈惟鲜
     * @date 2016年11月10日 下午12:51:04
     *
     */
    public class MyCollectionUtil {
        /**map转换为对象
         * 
         * @param map map信息
         * @param beanClass 对象类
         * @return
         * @throws Exception
         * @author 陈惟鲜
         * @date 2016年4月11日 上午11:05:40
         */
        public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
            if (map == null){
                return null;  
            }  
            Object obj = beanClass.newInstance();  
            Field[] fields = obj.getClass().getDeclaredFields();   
            for (Field field : fields) {    
                int mod = field.getModifiers();    
                if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){    
                    continue;    
                }    
                field.setAccessible(true);    
                field.set(obj, map.get(field.getName()));   
            }   
            return obj;    
        }  
        
        /**map转换为对象,包括对象父类全部赋值
         * 
         * @param map map信息
         * @param beanClass 对象类
         * @return
         * @throws Exception
         * @author 陈惟鲜
         * @date 2016年4月11日 上午11:05:40
         */
        public static Object mapToObjectAll(Map<String, Object> map, Class<?> beanClass) throws Exception {    
            if (map == null){
                return null;  
            }  
            Object obj = beanClass.newInstance();  
            // 获取所有父类的信息定义
            for(Class<?> clazz = obj.getClass(); clazz != Object.class ; clazz = clazz.getSuperclass()) {  
                  Field[] fields = clazz.getDeclaredFields();   
                  for (Field field : fields) {    
                      int mod = field.getModifiers();    
                      if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){    
                          continue;    
                      }    
                      field.setAccessible(true);    
                      field.set(obj, map.get(field.getName()));   
                  } 
            }  
       
            return obj;    
        }    
       
        /**对象转换为map
         * 
         * @param obj
         * @return
         * @throws Exception
         * @author 陈惟鲜
         * @date 2016年4月11日 上午11:05:59
         */
        public static Map<String, Object> objectToMap(Object obj) throws Exception {    
            if(obj == null){    
                return null;    
            }   
            Map<String, Object> map = new HashMap<String, Object>();    
            Field[] declaredFields = obj.getClass().getDeclaredFields();    
            for (Field field : declaredFields) {    
                field.setAccessible(true);  
                map.put(field.getName(), field.get(obj));  
            }    
            return map;  
        }
        
        /**对象转换为map,b
         * 
         * @param obj
         * @return
         * @throws Exception
         * @author 陈惟鲜
         * @date 2016年4月11日 上午11:05:59
         */
        public static Map<String, Object> objectToMapAll(Object obj) throws Exception {    
            if(obj == null){    
                return null;    
            }   
            Map<String, Object> map = new HashMap<String, Object>();       
            for(Class<?> clazz = obj.getClass(); clazz != Object.class ; clazz = clazz.getSuperclass()) {  
                Field[] fields = clazz.getDeclaredFields();   
                  for (Field field : fields) {    
                      field.setAccessible(true);    
                      map.put(field.getName(), field.get(obj));  
                  } 
          }
            return map;  
        }
        
        public static void main(String[] args) {
            PageResultVo pageResultVo = new PageResultVo();
            PageVo pageVo = new PageVo();
            pageVo.setCount(50);
            pageVo.setPageSize(500);
            List<PageVo> resultList = new ArrayList<PageVo>();
            resultList.add(pageVo);
            pageResultVo.setPage(pageVo);
            pageResultVo.setResultList(resultList);
            try {
                Map map = objectToMap(pageResultVo);
                System.out.println(map);
                PageVo pageVo2 = (PageVo) mapToObject(map, PageVo.class);
                System.out.println(pageVo2.getPageSize());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
  • 相关阅读:
    MySQL数据库操作(4)表约束
    通过C学Python(3)列表的常用方法
    MySQL数据库操作(2)基本操作
    通过C学Python(1)关于语言、数值类型和变量
    通过C学Python(2)序列类型
    MySQL数据库操作(1)用户与权限
    一个文章单词分析作业
    pyCharm的几个配置
    MySQL数据库操作(3)表结构操作
    shell脚本中的for循环和while循环
  • 原文地址:https://www.cnblogs.com/a393060727/p/6050708.html
Copyright © 2011-2022 走看看