/**
     * 将一个 JavaBean 对象转化为一个  Map
     * @param bean 要转化的JavaBean 对象
     * @return 转化出来的  Map 对象
  * @throws Exception
     */

public static Map convertBean(Object bean){
      Map returnMap = new HashMap();
     if(bean==null){
      return returnMap;
     }
        Class type = bean.getClass();
      
        BeanInfo beanInfo;
  try {
   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 = null;
     try {
      result = readMethod.invoke(bean, new Object[0]);
     } catch (Exception e) {
      //DevLog.error(type.getName() + "转换成Map出错,方法:" + readMethod.getName(), e);
     }
                 if (result != null) {
                     returnMap.put(propertyName, result);
                 } else {
                     returnMap.put(propertyName, "");
                 }
             }
         }
  } catch (IntrospectionException e1) {
   DevLog.error(type.getName() + "转换成Map出错:", e1);
  }
       
        return returnMap;
    }

/**
     * 将一个 Map 对象转化为一个 JavaBean
     * @param type 要转化的类型
     * @param map 包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @throws Exception
     */

public static Object convertMap(Class type, Map map) throws Exception {                 BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性             Object obj = type.newInstance(); // 创建 JavaBean 对象

// 给 JavaBean 对象的属性赋值         PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();         for (int i = 0; i< propertyDescriptors.length; i++) {             PropertyDescriptor descriptor = propertyDescriptors[i];             String propertyName = descriptor.getName();             if (map.containsKey(propertyName)) {

     // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。                 Object value = map.get(propertyName);

Object[] args = new Object[1];

      args[0] = value;

    descriptor.getWriteMethod().invoke(obj, args);

}

}         return obj;

}

javabean与map互转的更多相关文章

  1. javaBean与Map<String,Object>互转

    背景:有时候想不通阿帕奇的BeanUtils是怎么进行map和Bean互相转化的. 工作闲暇之余,自己写个一小段代码,一探究竟,试试才发现,原来一切并非我们想的那么什么和复杂. 注:这里只是简单实例, ...

  2. JavaBean与Map<String,Object>相互转换

    一.为什么要实现javaBean与Map<String,Object>相互转换 Spring中的BaseCommandController对象可以将传递过来的参数封装到一个JavaBean ...

  3. 用第三方工具类,将JavaBean、List、Map<String,Object>转成JSON文本

    导入第三方jar包: >commons-beanutils-1.7.0.jar >commons-collections-3.1.jar >commons-lang-2.5.jar ...

  4. List<Map<String, Object>> 与 json 互转

    近期做指纹识别,需要用到缓存文件,数据量并不大,用redis不合适,所以用到了txt文件. 思路是 1.定时查询指纹,存到txt缓存文件中.      2.新增或删除指纹时,查询指纹,存到txt缓存文 ...

  5. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  6. 使用 JDBC 和 JavaTemplate 查询SQL语句返回 List<Map<String,Object>>

    使用JDBC执行sql语句返回List 类型: public class JdbcUtil { private static Log log = LogFactory.getLog(JdbcUtil. ...

  7. List<Map<String,Object>>使用Java代码遍历

    List<Map<String,Object>>的结果集怎么使用Java代码遍历以获取String,Object的值: package excel; import java.u ...

  8. java中对List<Map<String,Object>>中的中文汉字排序

    import java.text.Collator;import java.util.ArrayList;import java.util.Collections;import java.util.C ...

  9. java实现map和object互转的三种方法

    /**  * 使用org.apache.commons.beanutils进行转换  */  class A {              public static Object mapToObje ...

  10. 迭代输出Map和List<Map<String,Object>>的方法

    一.Map<String,Object> String:key的类型 Object:value的类型,value可能是String,或者int类型,什么类型都可以 对于Map接口来说,本身 ...

随机推荐

  1. 基于Berkeley DB实现的持久化队列

    转自:http://guoyunsky.iteye.com/blog/1169912 队列很常见,但大部分的队列是将数据放入到内存.如果数据过多,就有内存溢出危险,而且长久占据着内存,也会影响性能.比 ...

  2. mysql中datetime比较大小问题 (转的)

    方法一: 你也可以:select * from t1 where unix_timestamp(time1) > unix_timestamp('2011-03-03 17:39:05') an ...

  3. iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]

    自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...

  4. _jobdu_1002

    /************************************************************************/ /* 题目1002:Grading 时间限制:1 ...

  5. [转]Calling Web Service Functions Asynchronously from a Web Page 异步调用WebServices

    本文转自:http://www.codeproject.com/Articles/70441/Calling-Web-Service-Functions-Asynchronously-from Ove ...

  6. CSS兼容性

    1,浮动 ie6中,设置浮动的元素,width自动包裹内容了.通常我们要设定一下这个元素的宽度 子元素浮动父容器高度不能自适应的CSS解决方法 网页前端工作者经常会遇到子元素设置float浮动后导致父 ...

  7. java数组排序问题:array.sort()是从小到大排序,那么如何从大到小排序?

    别告诉我从i=a.length开始打印然后i--!因为数组没变啊,只是打印顺序变了.有木有啥别的方法,除了冒泡插入选择.. nteger [] array=new Integer[]{1,2,3,4, ...

  8. Web 在线文件管理器学习笔记与总结(8)删除文件

    unlink($filename) 删除文件 index.php: <?php require 'dir.func.php'; require 'file.func.php'; require ...

  9. 以http形式启动uwsgi服务

    uwsgi yourfile.ini # 配置文件 [uwsgi] http = 127.0.0.1:3106 socket = 127.0.0.1:3006 chdir = /www/student ...

  10. vi 编辑器常用命令

    VI编辑器可以分为3种状态,它们分别是命令模式.输入模式以及末行模式,VI运行后默认进入命令模式. 命令模式:控制屏幕光标的移动,字符.单词或行的删除.替换,复制粘贴数据以及由此进入插入模式和末行模式 ...