欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Bean与Map之间相互转化的实现

程序员文章站 2022-07-02 15:20:12
Apache的BeanUtils Bean工具类很强大,基本涵盖了Bean操作的所有方法。这里的话我们就讲讲两个方面,一是Bean covert to Map,二是Map covert to Bean;Bean转Map其实利用的是Java的动态性-Reflection,不管是什么Bean通过动态解析... ......

目录树

  • 概述
  • apache beanutils将bean转map
  • apache beanutils将map转bean
  • 理解beanutils将bean转map的实现之手写bean转map

 


 

概述

  apache的beanutils bean工具类很强大,基本涵盖了bean操作的所有方法。这里的话我们就讲讲两个方面,一是bean covert to map,二是map covert to bean;bean转map其实利用的是java的动态性-reflection技术,不管是什么bean通过动态解析都是可以转成map对象的,但前提条件是field需要符合驼峰命名不过这也是写码规范,另一个条件就是每个field需要getter、setter方法。而map转bean一样也是通过reflection动态解析成bean。java的reflection其实是挺重要的,我们用的很多工具类都有它的存在,我们不止要会用而且更重要的是能够理解是为什么,最好是自己去手写实现这样的话更能加深理解。

 


 

用apache beanutils将bean转map

  • 代码实现
 1  /**
 2      * 用apache的beanutils实现bean covert to map
 3      * @throws exception
 4      */
 5     public static void beantomap() throws exception {
 6         user user=new user();
 7         map<string,string> keyvalues=null;
 8 
 9         user.setpassword("password");
10         user.setcomments("test method!");
11         user.setusername("wang shisheng");
12         user.setcreatetime(new date());
13 
14         keyvalues=beanutils.describe(user);
15 
16         logger.info("bean covert to map:{}", jsonobject.tojson(keyvalues).tostring());
17     }

 

  • 测试结果

Java Bean与Map之间相互转化的实现

 

 


 

用apache beanutils将map转bean

  • 代码实现
 1 /**
 2      * 用apache的beanutils实现map covert to bean
 3      * @throws exception
 4      */
 5     public static void maptobean() throws exception {
 6         map<string,string> keyvalues=new hashmap<>();
 7         user user=new user();
 8 
 9         keyvalues.put("sessionid","ed442323232ff3");
10         keyvalues.put("username","wang shisheng");
11         keyvalues.put("password","xxxxx44333");
12         keyvalues.put("requestnums","34");
13 
14         beanutils.populate(user,keyvalues);
15 
16         logger.info("map covert to bean:{}", user.tostring());
17     }

 

  • 测试结果

Java Bean与Map之间相互转化的实现

 


 

理解beanutils将bean转map的实现之手写bean转map

  • 代码实现
/**
     * 应用反射(其实工具类底层一样用的反射技术)
     * 手动写一个 bean covert to map
     */
    public static void autobeantomap(){
        user user=new user();
        map<string,object> keyvalues=new hashmap<>();

        user.setpassword("password");
        user.setcomments("test method!");
        user.setusername("wang shisheng");
        user.setusercode("2018998770");
        user.setcreatetime(new date());

        method[] methods=user.getclass().getmethods();
        try {
            for(method method: methods){

                string methodname=method.getname();
                //反射获取属性与属性值的方法很多,以下是其一;也可以直接获得属性,不过获取的时候需要用过设置属性私有可见
                if (methodname.contains("get")){
                    //invoke 执行get方法获取属性值
                    object value=method.invoke(user);
                    //根据setxxxx 通过以下算法取得属性名称
                    string key=methodname.substring(methodname.indexof("get")+3);
                    object temp=key.substring(0,1).tostring().tolowercase();
                    key=key.substring(1);
                    //最终得到属性名称
                    key=temp+key;
                    keyvalues.put(key,value);
                }
            }
        }catch (exception e){
            logger.error("错误信息:",e);
        }

        logger.info("auto bean covert to map:{}", jsonobject.tojson(keyvalues).tostring());


    }

 

  • 测试结果

Java Bean与Map之间相互转化的实现