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); }}
- Bean和Map的互转+Demo
- java中的bean、list、map和JSON的互转
- map和bean互转工具
- map和bean的故事
- Bean类和Map的相互转换
- Java Map和Bean之前的转换
- Map 和 java Bean 的相互转换
- Map to Bean map 转 bean
- Object类型的Bean转Map
- Java学习笔记(二十九):Map转Bean和Bean转Map
- map 转 bean
- java Bean转Map
- java map 转 bean
- java map转bean
- java-map转bean
- Map的排序Demo
- java 利用内省实现bean和map的转换
- java bean、List、数组、map和Json的相互转化
- HDU 3642 Get The Treasury 【线段树】【扫描线】
- opencv如何进行摄像头标定
- hbase安装说明
- opencv学习(十四)之图像颜色通道分离和融合
- Java金融信息交换引擎 QuickFIX/J
- Bean和Map的互转+Demo
- 狡猾的商人_bzoj1202_差分约束
- Hadoop安装说明
- c#中单元测试
- java基础之equal()方法、static关键字、单例设计模式
- bridge和数码相片处理方式
- R语言快餐教程(1) - 我们开始做统计吧
- http请求方法(GET、POST)
- 使用IDEA进行Spark开发(一)-IDEA的配置