Bean和Map的互转+Demo

事情原由:由于数据库空值字段要作为key值保存在Map中,之前写过一篇通过mybatis配置,可以解决这样的问题,但是测试环境时因为其他表空值的原因就爆炸了,只有老老实实的通过Bean转化Map。

其中涉及的类:
Introspector:在java.beans包下,是Java 语言对 JavaBean 类属性、事件的一种缺省处理方法。
JavaBean:一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。如果在两个模块之间传递信息,可以将信息封装进JavaBean中,这种对象称为“值对象”(Value Object),或“VO”。方法比较少。这些信息储存在类的私有变量中,通过set()、get()获得。
PropertyDescriptor:表示JavaBean类通过存储器导出一个属性。主要方法:
1. getPropertyType(),获得属性的Class对象;
  2. getReadMethod(),获得用于读取属性值的方法;getWriteMethod(),获得用于写入属性值的方法;
  3. hashCode(),获取对象的哈希值;
  4. setReadMethod(Method readMethod),设置用于读取属性值的方法;
  5. setWriteMethod(Method writeMethod),设置用于写入属性值的方法。

需要注意的是:javaBean转map时,如果javaBean的属性是另外一个javaBena会有问题:

{dbs=com.cn.bean2map.DemoBeanSon@1ee7b241, name=chen, age=24}

转化类:

public class MapBean {

    public static final Map<String, Object> beanToMap(Object bean)  
            throws IntrospectionException, IllegalAccessException, InvocationTargetException {  
        Map<String, Object> returnMap = new HashMap<String, Object>();  
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());  
        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 = readMethod.invoke(bean, new Object[0]);  
                if (result != null) {  
                    returnMap.put(propertyName, result);  
                } else {  
                    returnMap.put(propertyName, "");  
                }  
            }  
        }  
        return returnMap;  
    }  

    public static final Object mapToBean(Class<?> type, Map<String, ? extends Object> map)   
            throws IntrospectionException, IllegalAccessException,  InstantiationException, InvocationTargetException {  
        BeanInfo beanInfo = Introspector.getBeanInfo(type);  
        Object obj = type.newInstance();  
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
        for (int i = 0; i< propertyDescriptors.length; i++) {  
            PropertyDescriptor descriptor = propertyDescriptors[i];  
            String propertyName = descriptor.getName();  
            if (map.containsKey(propertyName)) {  
                Object value = map.get(propertyName);  
                Object[] args = new Object[1];  
                args[0] = value;  
                descriptor.getWriteMethod().invoke(obj, args);  
            }  
        }  
        return obj;  
    }  

}

实体类DemoBean:

public class DemoBean {
    public String name;

    public Integer age;

    public DemoBeanSon dbs;

    public DemoBean(String name,Integer age,DemoBeanSon dbs){
        this.name=name;
        this.age=age;
        this.dbs=dbs;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public DemoBeanSon getDbs() {
        return dbs;
    }

    public void setDbs(DemoBeanSon dbs) {
        this.dbs = dbs;
    }

    public String toString(){
        return "name为:"+name+"===="+"age为:"+age+"dbs为:"+dbs.phone+"---"+dbs.num;
    }
}

实体DemoBeanSon:

public class DemoBeanSon {

    public String phone;

    public Integer num;

    public DemoBeanSon(String phone,Integer num){
        this.phone=phone;
        this.num=num;
    }
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }


}

mian方法:

public class DemoMain {

    public static void main(String[] args)
            throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
        /*DemoBean db = new DemoBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "chen");
        map.put("age", 24);
        map.put("dbs", new DemoBeanSon("小米",4));
        db=(DemoBean) MapBean.mapToBean(db.getClass(), map);
        System.out.println(db);*/

        DemoBean db = new DemoBean("chen",24,new DemoBeanSon("小米",4));
        Map map=MapBean.beanToMap(db);
        System.out.println(map);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值