Using Java reflection mechanism to realize list<map<string, object>> conversion to list<javabean>

Source: Internet
Author: User

Recently in the project with MyBatis, encountered the date format display to the Easyui problem, need to list<map<string, object>> into List<javabean> Finally found a method on the Internet:

The core approach is as follows:

[Java]View PlainCopy
  1. /**
  2. * object>> data is converted to JavaBean data according to List<map<string
  3. * @param datas
  4. * @param beanclass
  5. * @return
  6. * @throws commonexception
  7. */
  8. Public list<t> Listmap2javabean (list<map<string, object>> datas, class<t> beanClass) Throws Commonexception {
  9. //Return data collection
  10. list<t> list = null;
  11. //Object field name
  12. String fieldname = "";
  13. //object method name
  14. String methodname = "";
  15. //Object methods need to assign values
  16. Object Methodsetvalue = "";
  17. try {
  18. List = new arraylist<t> ();
  19. //Get all fields of the object
  20. Field fields[] = Beanclass.getdeclaredfields ();
  21. //Traverse data
  22. for (map<string, object> mapdata:datas) {
  23. //Create a generic type instance
  24. T t = beanclass.newinstance ();
  25. //Traverse all fields, corresponding to the configured fields and assign values
  26. For (Field field:fields) {
  27. //Get annotation configuration
  28. JavaBean JavaBean = field.getannotation (JavaBean.   Class);
  29. if (null! = JavaBean) { //has annotation configuration, next action
  30. //Convert all to uppercase
  31. String dbfieldname = Javabean.dbfieldname (). toUpperCase ();
  32. //Get field name
  33. FieldName = Field.getname ();
  34. //Splicing Set method
  35. MethodName = "Set" + strutil.capitalize (fieldname);
  36. //Get the corresponding value in data
  37. Methodsetvalue = Mapdata.get (dbfieldname);
  38. //Assign To field
  39. Method m = Beanclass.getdeclaredmethod (methodname, Field.gettype ());
  40. M.invoke (t, Methodsetvalue);
  41. }
  42. }
  43. //Deposit back to list
  44. List.add (t);
  45. }
  46. } catch (Instantiationexception e) {
  47. throw New Commonexception (E, "Create Beanclass instance exception");
  48. } catch (Illegalaccessexception e) {
  49. throw New Commonexception (E, "Create Beanclass instance exception");
  50. } catch (SecurityException e) {
  51. throw New Commonexception (E, "get [" + FieldName + "] Getter Setter Method Exception");
  52. } catch (Nosuchmethodexception e) {
  53. throw New Commonexception (E, "get [" + FieldName + "] Getter Setter Method Exception");
  54. } catch (IllegalArgumentException e) {
  55. throw New Commonexception (E, "[" + methodname + "] Method Assignment Exception");
  56. } catch (InvocationTargetException e) {
  57. throw New Commonexception (E, "[" + methodname + "] Method Assignment Exception");
  58. }
  59. //Return
  60. return list;
  61. }

Using Java reflection mechanism to realize list<map<string, object>> conversion to list<javabean>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.