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.