Map --> Bean 2: using the org.apache.commons.beanutils tool class for Map --> bean public static void transmap2bean2 (Map<String, object> map, object obj) { if (map == null | | obj == null) { return; } try { beanutils.populate (Obj, map); } catch (exception e) { system.out.println ("transmap2bean2 error " + e); } } // Map --> Bean 1: using Introspector, PropertyDescriptor Implementation map --> bean public static void transmap2bean (map<string, object> map, object obj) { try { BeanInfo beanInfo = Introspector.getbeaninfo (Obj.getclass ()); propertydescriptor[] propertydescriptors = beaninfo.getpropertydescriptors (); for ( Propertydescriptor property : propertydescriptors) { string key = property.getname (); if (Map.containskey (key)) { object value = map.get (Key); // Get the property corresponding setter method method setter = property.getwritemethod (); setter.invoke (Obj,&nbsP;value); } } } catch (Exception e) { system.out.println ( "transmap2bean error " + e); } return; } // bean --> map 1: using Introspector and propertydescriptor will bean --> map Public static map<string, object> transbean2map (object obj) { &Nbsp; if (obj == null) { return null; } Map<String, Object> map = new HashMap<String, Object> (); try { beaninfo beaninfo = introspector.getbeaninfo ( Obj.getclass ()); Propertydescriptor[] propertydescriptors = beaninfo.getpropertydescriptors (); for (Propertydescriptor property : propertydescriptors) { String key = property.getname (); // Filter Class Properties if (!key.equals ("Class")) { // get the property corresponding getter method method getter = property.getreadmethod (); object value = getter.invoke (obj); map.put (Key, value); } } } catch (Exception e) { System.out.println ("transbean2map error " + e); } return map; }
This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1686654
Java map and the transformation before the bean