Map和Bean的相互转换
BeanUtils位于org.apache.commons.beanutils.BeanUtils下面,其方法populate的作用解释如下:
完整方法:
BeanUtils.populate( Object bean, Map properties ),
这个方法会遍历map<key, value>中的key,如果bean中有这个属性,就把这个key对应的value值赋给bean的属性。
1、bean 转换成 map
 Person person1=new Person();  
        person1.setName("name1");  
        person1.setSex("sex1");  
        Map<String, String> map=null;  
        try {  
            map = BeanUtils.describe(person1);  
2、map 转换成 bean
 /** 
     *  
     *  
     * Map转换层Bean,使用泛型免去了类型转换的麻烦。 
     * @param <T> 
     * @param map   
     * @param class1 
     * @return 
     */  
    public static <T> T map2Bean(Map<String, String> map, Class<T> class1) {  
        T bean = null;  
        try {  
            bean = class1.newInstance();  
            BeanUtils.populate(bean, map);  
        } catch (InstantiationException e) {  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            e.printStackTrace();  
        } catch (InvocationTargetException e) {  
            e.printStackTrace();  
        }  
        return bean;  
    }  

Map和Bean的相互转换的更多相关文章

  1. Ambiguous mapping found. Cannot map 'xxxxController' bean method

    1.背景 今天要做一个demo,从github上clone一个springmvc mybatis的工程(https://github.com/komamitsu/Spring-MVC-sample-u ...

  2. Json与bean的相互转换

    本文使用json-lib jar包实现Json与bean的相互转换 1.将字符串转为JSON 使用JSONObject.fromObject(str)方法即可将字符串转为JSON对象 使用JSONOb ...

  3. 利用jaxb实现xml和bean的相互转换

    1.使用jar包生成xsd文件 java -jar trang.jar a.xml a.xsd xml格式 生成的xsd文件 2.使用xjc命令生成bean文件 xjc a.xsd 生成的相关bean ...

  4. SpringMVC“Ambiguous mapping found. Cannot map 'XXXController' bean method”解决方法

    [转 :http://www.fanfanyu.cn/news/staticpagefile/2351.html] 最近在开发项目的过程中SpringMVC抛了个"Ambiguous map ...

  5. @ConfigurationProperties绑定配置信息至Array、List、Map、Bean

    原文:https://blog.csdn.net/justry_deng/article/details/90758250 相关说明:在SpringBoot中,我们可以通过以下几种方式获取并绑定配置文 ...

  6. Bean与Map的转换 和 Map与Bean的转换

    package com.JUtils.beanConvert; import java.beans.BeanInfo; import java.beans.IntrospectionException ...

  7. java Map与Bean相互转化

    import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector ...

  8. 007-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐,随机字母数字串

    一.转换 1.1.TreeMap 有序Map 无序有序转换 使用默认构造方法: public TreeMap(Map<? extends K, ? extends V> m) 1.2.Ma ...

  9. Ambiguous mapping found. Cannot map 'competeController' bean method

    报错: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMapp ...

随机推荐

  1. 树上启发式合并(dsu on tree)学习笔记

    有丶难,学到自闭 参考的文章: zcysky:[学习笔记]dsu on tree Arpa:[Tutorial] Sack (dsu on tree) 先康一康模板题吧:CF 600E($Lomsat ...

  2. springboot2 统一异常处理

    统一异常处理,不需要在每一层上单独捕获异常,只需要关注业务的开发: 代码如下: @RestControllerAdvice @Slf4j public class GlobalExceptionHan ...

  3. pairs 和 ipairs 的区别

    ipairs 在迭代过程中是会直接跳过所有手动设定key值的变量.pairs不会跳过手动设置key值的变量. 实例 tab = {,,a="cd","d"} f ...

  4. 关于java中ArrayList的快速失败机制的漏洞——使用迭代器循环时删除倒数第二个元素不会报错

    一.问题描述 话不多说,先上代码: public static void main(String[] args) throws InterruptedException { List<Strin ...

  5. mybatis 环境搭建和基本说明

    mybatis介绍就不多提了,直接步入正题. 先准备好eclipse和MySQL,然后先看一下目录结构 文件和类很少,所以mybatis的搭建是非常简单的,如搭建中遇到问题可以先参考文档最后一部分的综 ...

  6. 获取文本中所有的<img>标签的位置,获取所有img标签的src

    public static int[] GetImagePos(string str) { str = str.Replace("$", " "); str = ...

  7. java网络编程之Socket编程

    概念 网络编程分为BIO(传统IO).NIO.AIO.Socket编程属于BIO这种传统IO. InetAddress java.net.InetAddress是JAVA中管理IP地址的类,常用 pu ...

  8. UVA-11761-马尔可夫/记忆化搜索

    https://vjudge.net/problem/UVA-11762 给出一个整数n,每次随机挑选一个小于等于n的素数,如果是n的因子,n变为n/x ,否则不变,问n变为1的期望挑选次数. f[i ...

  9. 谈谈oracle里的join、left join、right join、full join-版本2

    --1.left join  左表为主表,左表返回全部数据,右表只返回与左表相匹配的数据select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje ...

  10. Granting and Managing Item Level Permission using SharePoint2013 Designer Workflow

    https://gnanasivamgunasekaran.wordpress.com/2015/12/29/granting-and-managing-item-level-permission-u ...