java Bean转Map
来源:互联网 发布:google for mac 编辑:程序博客网 时间:2024/04/28 23:06
代码如下:
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;import cn.ruis.rscms.model.Website;public class BeanMapUtil {/** * 将一个 JavaBean 对象转化为一个 Map * * @param bean * 要转化的JavaBean 对象 * @return returnMap 转化出来的 Map 对象 */public static <T> Map<String, Object> beanToMap(T bean) {Class<? extends Object> type = bean.getClass();Map<String, Object> returnMap = new HashMap<String, Object>();try {BeanInfo beanInfo = Introspector.getBeanInfo(type);PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();for (PropertyDescriptor descriptor : propertyDescriptors) {String propertyName = descriptor.getName();if (!propertyName.equals("class")) {Method readMethod = descriptor.getReadMethod();Object result = readMethod.invoke(bean, new Object[0]);//returnMap.put(propertyName, result != null ? result : "");if(result != null)returnMap.put(propertyName, result);elsecontinue;}}} catch (IntrospectionException e) {throw new RuntimeException("分析类属性失败", e);} catch (IllegalAccessException e) {throw new RuntimeException("分析类属性失败", e);} catch (InvocationTargetException e) {throw new RuntimeException("分析类属性失败", e);}return returnMap;}/** * 将一个Map对象转化为一个JavaBean * * @param map * 包含属性值的map * @param bean * 要转化的类型 * @return beanObj 转化出来的JavaBean对象 */public static <T> T mapToBean(Map<String, Object> paramMap, Class<T> clazz) {T beanObj = null;try {beanObj = clazz.newInstance();String propertyName = null;Object propertyValue = null;for (Map.Entry<String, Object> entity : paramMap.entrySet()) {propertyName = entity.getKey();propertyValue = entity.getValue();setProperties(beanObj, propertyName, propertyValue);}} catch (IllegalArgumentException e) {throw new RuntimeException("不合法或不正确的参数", e);} catch (IllegalAccessException e) {throw new RuntimeException("实例化JavaBean失败", e);} catch (Exception e) {throw new RuntimeException("Map转换为Java Bean对象失败", e);}return beanObj;}public static <T> void setProperties(T entity, String propertyName,Object value) throws IntrospectionException,IllegalArgumentException, IllegalAccessException,InvocationTargetException {PropertyDescriptor pd = new PropertyDescriptor(propertyName, entity.getClass());Method methodSet = pd.getWriteMethod();methodSet.invoke(entity, value);}/** * 测试 能否将类转换成Mpa * @param args */public static void main(String[] args) {Website w = new Website();w.setId(1);w.setWebsite4Unti("xxx");w.setWebsiteName("asdf");w.setWebsiteUrl("http://www.baidu.com");Map<String, Object> um = BeanMapUtil.beanToMap(w);for (Map.Entry<String, Object> entity : um.entrySet()) {}}}
0 0
- java Bean转Map
- java map 转 bean
- java map转bean
- java-map转bean
- java map转java bean对象
- Java json转Map,转bean,转List<bean>
- 【java】Map List Bean 转 JSON
- Map to Bean map 转 bean
- Java学习笔记(二十九):Map转Bean和Bean转Map
- map 转 bean
- java Map与bean互转
- java基于反射的Map转Bean的工具类
- java基于反射的Map转Bean的工具类
- java中的bean、list、map和JSON的互转
- 浅谈java Map 和java Bean
- Map转bean多类型转换,bean转map
- List<map>转List<bean>
- JAVA Map转换为Bean或VO
- 看项目代码,有些类方法不明白,去查API又找不到这个方法。
- C++个人未解决笔记或未熟悉的使用
- 【观】openWRT --- 嵌入式Linux发行版目前在智能路由器应用比较火
- UpdateData、Invalidate、InvalidateRect和UpdateWindow及RedrawWindow
- 第6条:显式禁止编译器为生成不必要的函数
- java Bean转Map
- 分析Cocos2d-x横版ACT手游源代码 3、人物选择界面
- jjklsdjdosj;so
- sound engineers or music lovers
- Microsoft.AlphaImageLoader滤镜讲解
- Linux硬盘档案管理
- nyoj975关于521
- Dp_关于最大子矩阵的问题总结
- 解决等概率随机抽样问题