Design of JavaBean tool class for map automatic assembly based on Java reflection

Source: Internet
Author: User

We usually use the myabtis when not often need to use map to pass parameters, is generally the following steps:

Public list<role> findroles (map<string,object> param); <select id= "Dindroles" parametertype= "Map" resulttype= "Role" >    select Id,role_name as rolename,note form t_role    where role_name = #{rolename}    and Note = #{note}</select>//We often need to hand these field names map<string,object> map = new hashmap<> (); Map.put ("RoleName", "Xiaoming"); Map.put ("Note", "10"); list<role> roles = Rolemapper.findroles (map);

But if you need to put a lot of fields, and each property name is very long, it is very difficult to get, here can use the Java reflection method to automatically assemble the map, the following is the author's implementation:

public class Test {//Implement effect public static void main (string[] args) {hashmap<string, object> map = new H        Ashmap<> ();        Person Person1 = new person ();        Person1.setage (2);        Person1.setname ("Foonsu");        Call the written Mapbuild auto assemble mapbuild (person1, map); Output effect for (Map.entry Entry:map.entrySet ()) {System.out.println (Entry.getkey () + ":" + entry.getvalue        ());        }} public static void Mapbuild (Object javaBean, map map) {Class clazz = Javabean.getclass ();        Reflection gets field[] name = Clazz.getdeclaredfields (); For Field Field:name {//To assemble a non-null JavaBean attribute value into the map if (Getgetmethod (Javabean,field.getname ())!=n        ull) Map.put (Field.getname (), Getgetmethod (Javabean,field.getname ()));  }}/** * Based on properties, get Get method */public static Object Getgetmethod (object ob, String name) {method[] m =        Ob.getclass (). GetMethods ();       try {     for (int i = 0; i < m.length; i++) {if (("get" + name). toLowerCase (). Equals (M[i].getname (). ToLower                Case ())) {return M[i].invoke (OB);    }}} catch (Exception e) {} return null;    }}class person{int age;    String name;    String personId;    Public String Getpersonid () {return personId;    } public void Setpersonid (String personId) {This.personid = PersonId;    } public int Getage () {return age;    public void Setage (int.) {this.age = age;    } public String GetName () {return name;    } public void SetName (String name) {this.name = name; }}

Ps: In this is the direct mining traversal method, the time complexity is O (n^2), because in the actual production of a JavaBean attribute design is not very much, in fact, can also use the idea of space-time to optimize the method to O (N) time complexity.

Related Article

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.