1 PackageCom.studey.mapTbean;2 3 ImportJava.beans.BeanInfo;4 Importjava.beans.IntrospectionException;5 ImportJava.beans.Introspector;6 ImportJava.beans.PropertyDescriptor;7 Importjava.lang.reflect.InvocationTargetException;8 ImportJava.lang.reflect.Method;9 ImportJava.util.HashMap;Ten ImportJava.util.Map; One A Public classBeanutil { - - Public Static voidMain (string[] args)throwsException { the -Map<string, object> MP =NewHashmap<string, object>(); -Mp.put ("name", "Tom Cat"); -Mp.put ("addr", 178); +Mp.put ("tel", "123456"); - +Personbean person = Map2bean (MP, Personbean.class); ASystem.out.println ("Transmap2bean Map Info:"); at for(Map.entry<string, object>Entry:mp.entrySet ()) { -System.out.println (Entry.getkey () + ":" +Entry.getvalue ()); - } -System.out.println ("Bean Info:"); -System.out.println ("Name:" +person.getname ()); -System.out.println ("Addr:" +person.getaddr ()); inSystem.out.println ("MN:" +Person.gettel ()); - to Bean2map (person, MP); +System.out.println ("Transbean2map Map Info:"); - for(Map.entry<string, object>Entry:mp.entrySet ()) { theSystem.out.println (Entry.getkey () + ":" +Entry.getvalue ()); * } $ }Panax Notoginseng - //Maptobean Method: the + Public Static<t, K, v> T Map2bean (map<k, v> MP, class<t>beancls) A throwsException, IllegalArgumentException, invocationtargetexception { theT t =NULL; + Try { - $BeanInfo BeanInfo =Introspector.getbeaninfo (BEANCLS); $propertydescriptor[] PropertyDescriptors =beaninfo.getpropertydescriptors (); - -t =beancls.newinstance (); the - for(PropertyDescriptor property:propertydescriptors) {WuyiString key =property.getname (); the - if(Mp.containskey (key)) { WuObject value =Mp.get (key); -Method setter = Property.getwritemethod (); About
$ Setter.invoke (t, value); - } - } - A}Catch(introspectionexception e) { + the e.printstacktrace (); - } $ returnT; the } the the //Beantomap Method: the - Public Static<t, K, v> map<string, object> bean2map (T Bean, map<string, object>MP) in throwsException, illegalaccessexception { the the if(Bean = =NULL) { About return NULL; the } the the Try { +BeanInfo BeanInfo =Introspector.getbeaninfo (Bean.getclass ()); -propertydescriptor[] PropertyDescriptors =beaninfo.getpropertydescriptors (); the Bayi for(PropertyDescriptor property:propertydescriptors) { theString key =property.getname (); the - if(!key.equals ("Class")) { - theMethod getter = Property.getreadmethod ();
the
the Object value; the -Value =Getter.invoke (bean); the mp.put (key, value); the } the 94 } the the}Catch(introspectionexception e) { the 98 e.printstacktrace (); About}Catch(Illegalaccessexception | IllegalArgumentException |invocationtargetexception e) { -
101 e.printstacktrace ();102 103 }104 returnMP; the 106 }107}
1 PackageCom.studey.mapTbean;2 3 Public classPersonbean {4 PrivateString name;5 PrivateInteger addr;6 PrivateString Tel;7 8 PublicString GetName () {9 returnname;Ten } One A Public voidsetName (String name) { - This. Name =name; - } the - PublicInteger getaddr () { - returnaddr; - } + - Public voidsetaddr (Integer addr) { + This. addr =addr; A } at - PublicString Gettel () { - returnTel; - } - - Public voidSettel (String tel) { in This. Tel =Tel; - } to +}
Operation Result:
1 Transmap2bean Map Info: 2 Name:tom Cat 3 tel:123456 4 addr:178 5Bean Info: 6name:tom Cat 7 A ddr:178 8 mn:123456 9transbean2map Map Info:name:tom Cat tel:123456 addr:178
Conversion of bean classes and maps in Java