Bean和Map的互转+Demo

来源:互联网 发布:留学低龄化的数据 编辑:程序博客网 时间:2024/06/03 17:36

事情原由:由于数据库空值字段要作为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