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. asp.net自定义控件之加载层

    知识点:JQuery.Ajax.自定义控件 该文旨在给大家开发自定义控件(结合js)一个思路,一个简单的示例,可能在实际项目中并不会这样做. 先来看看效果: 1.在静态页面里开发好想要的效果 jQue ...

  2. Google V8 引擎 原理详解

    V8 引擎概览 V8 引擎简介 Google V8 引擎使用 C++ 代码编写,实现了 ECMAScript 规范的第五版,可以运行在所有的主流 操作系统中,甚至可以运行在移动终端 ( 基于 ARM ...

  3. python爬虫scrapy命令工具学习之篇三

    命令行工具(Command line tools) 全局命令 startproject settings runspider shell fetch view version 项目命令 crawl c ...

  4. html 入门2-表

    html  入门-列表 表格 表单 一.表标签 1,无序列表 ( ul:li ) 注意:代码排版必须要层次分明 2,有序列表 (ol:li) 3,自定义列表 (dl:li) 二.表格标签 1,tabl ...

  5. ThinkPHP 表单自动验证运用

    使用TP 3.2框架 public function add_post(){ //验证规则 $rule=array( array('name','require','请输入姓名',1),//必须验证n ...

  6. Java中含有静态成员的的初始化顺序

    class Bowl{ Bowl(int marker){ System.out.println("Bowl(" + marker + ")" ); } voi ...

  7. 读书笔记:提高C++性能的编程技术

    Efficient C++ Performance Programming Techniques 第1章 跟踪范例 1.1 关注点 本章引入的实际问题为:定义一个简单的Trace类,将当前函数名输出到 ...

  8. Java回顾之集合

    在这篇文章里,我们关注Java中的集合(Collection).集合是编程语言中基础的一部分,Java自JDK早期,就引入了Java Collection Framework.设计JCF的那个人,后来 ...

  9. linux 查看日志最后几行

    tail -n 50 wx.log 示例:查看/var/log/boot.log,只显示最后一行.则执行 tail -n 1  /var/log/boot.log tail -n 1000:显示最后1 ...

  10. WPF 动画 和 色彩 的随笔

    1:善于用“Margin”做动画效果 2:色彩处理通常用:Brush,而Brush(如:SolidColorBrush)的实例化,通常需要载入“ System.Windows.Media.Color” ...