bean 与 map 互转.
来源:互联网 发布:哈工大 大数据 编辑:程序博客网 时间:2024/06/05 10:17
package com.sprucetec.tms.distribute.utils;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;/** * bean 与 map 互转. * * @author Yinqiang Du * @date 2016/10/8 */public class BeanToMapUtil { /** * 将一个 Map 对象转化为一个 JavaBean * @param type 要转化的类型 * @param map 包含属性值的 map * @return 转化出来的 JavaBean 对象 * @throws IntrospectionException * 如果分析类属性失败 * @throws IllegalAccessException * 如果实例化 JavaBean 失败 * @throws InstantiationException * 如果实例化 JavaBean 失败 * @throws InvocationTargetException * 如果调用属性的 setter 方法失败 */ public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性 Object obj = type.newInstance(); // 创建 JavaBean 对象 // 给 JavaBean 对象的属性赋值 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i< propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。 Object value = map.get(propertyName); Object[] args = new Object[1]; args[0] = value; descriptor.getWriteMethod().invoke(obj, args); } } return obj; } /** * 将一个 JavaBean 对象转化为一个 Map * @param bean 要转化的JavaBean 对象 * @return 转化出来的 Map 对象 * @throws IntrospectionException 如果分析类属性失败 * @throws IllegalAccessException 如果实例化 JavaBean 失败 * @throws InvocationTargetException 如果调用属性的 setter 方法失败 */ public static Map convertBean(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException { Class type = bean.getClass(); Map returnMap = new HashMap(); BeanInfo beanInfo = Introspector.getBeanInfo(type); 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; }}
0 0
- bean 与 map 互转.
- Util map与Bean互转
- java Map与bean互转
- Map与Bean互转(下划线转大写)
- list 、map、bean、json互转
- Java bean 与 Map的相互转换
- <转>BeanUtils Map与Bean转换
- map与bean和list<Map>与List<bean>互相转换
- jsonString 与bean互转
- Java中Bean与Map/Json/DBObject的相互转换
- java注解实现map与bean之间的转换
- nhmicro框架Map与bean映射(orm机制)
- 使用内省机制实现Map与Bean之间的转换
- Map to Bean map 转 bean
- map 转 bean
- bean转化成map
- spring map bean定义
- spring map bean定义
- RedisService
- Set Associative Cache
- 故事会-设计模式-抽象工厂
- GDB 格式化结构体输出及记录到文件
- mongodb添加删除节点
- bean 与 map 互转.
- Dlib人脸检测+对齐
- 在VS2013中出现 error C3861: “snprintf”: 找不到标识符
- Redis事务、锁应用、消息订阅
- greenplum 5.0 版本规划
- mysql运维与分析--show processlist
- eclipse 批量左移右移
- Http请求 post get
- 【大拿分享】单页应用(Single Page Application)的搜索引擎优化