Reprint Address: http://iyiguo.net/blog/2012/09/27/mybatis-param-mapping-rules/
rules
non-annotated parameters
When parameters are not using @param annotations, they can be accessed in the following ways:
#{parameter position [0..n-1]}
Or
#{param[1..n]}
If the parameter type is a custom object bean, simply add the. Object property to
#{parameter position [0..n-1]. Object Properties}
#{param[1..n]. Object Properties}
In particular, if there is only one non-annotation parameter, you can use the
#{any character} annotation parameter
@Param annotated with an argument is equivalent to specifying an alias for the parameter. The argument after the comment can only be passed
#{Note Alias}
Or
#{param[1..n]}
If the parameter type is a custom object bean, simply add the. Object property to access the object properties
#{annotation aliases. Properties}
#{param[1..n]. Properties} Example
In order to clarify the above rules, our example specifically breaks down various scenarios for presentation. non-annotated type a parameter
User Getuserbyid (int id); SELECT * from <TABLE> where id = #{id}//or select * from <TABLE> where id = #{abdc}//or select * FROM < table> WHERE id = #{param1} user getUser (user user); User.getname user.getage SELECT * from <TABLE> WHERE name = #{name} and age = #{age}
Multiple Parameters
User GetUser (String name, int age); SELECT * from <TABLE> WHERE name = #{0} and age = #{1}//or select * from <TABLE> WHERE name = #{param1} and Age = #{param2} user getUser (user usr, int flag); SELECT * from <TABLE> WHERE name = #{0.name} and age = {0.age} and flags = #{1}//or select * from <TABLE> whe Re name = #{param1.name} and age = {Param1.age} and flag = #{param2}
annotation Type
a parameter
User Getuserbyid (@Param (value= "keyId") int id); SELECT * from <TABEL> where id = #{keyid}//or select * from <TABLE> where id = #{param1} User getUser (@Param (value= "User") User user); User.getname user.getage SELECT * from <TABLE> WHERE name = #{user.name} and age = #{user.age}//or SELECT * Fro M <TABLE> WHERE name = #{param1.name} and age = #{param1.age}
Multiple Parameters
User GetUser (@Param (value= "XM") String name, @Param (value= "NL") int age); SELECT * from <TABLE> WHERE name = #{xm} and age = #{nl}//or select * from <TABLE> WHERE name = #{param1} an D age = #{param2}//or select * from <TABLE> WHERE name = #{xm} and age = #{param2} User getUser (@Param (value= "usr" User User, @Param (value= "tag") int flag); SELECT * from <TABLE> WHERE name = #{usr.name} and "Age = #{usr.age}" and "flag = #{tag}//or select * from <table& Gt WHERE name = #{param1.name} and age = #{param1.age} and flags = #{param2}//or select * from <TABLE> where name = #{ Usr.name} and age = #{param1.age} and flag = #{param2}
non-annotated and annotated mixed-type
When using @param annotations with partial parameters, parameter annotations are used to combine the above two cases.
User GetUser (String name, @Param (value= "NL") age, int gendar); Access to age cannot be #{1} can only be #{param2} | #{nl} select * from <TABLE> WHERE name = #{0} and age = #{nl} and Gendar = #{param3)
Framework main Map processing code
acquisition of Parameters
Org.apache.ibatis.binding.MapperMethod
Private Object GetParam (object[] args) {final int paramcount = Parampositions.size ();//parameterless if (args = = NULL | | paramcou NT = = 0) {return null;//no annotations with number of arguments 1} else if (!hasnamedparameters && ParamCount = 1) {return Args[parampositi Ons.get (0)]; } else {map<string, object> param = new mapperparammap<object> (); for (int i = 0; i < ParamCount; i++) {p Aram.put (Paramnames.get (i), args[parampositions.get (i)]); }//Issue #71, add param names as param1, param2...but ensure backward compatibility//This is the origin of #{param[1..n]} for (int i = 0; i < ParamCount; i++) {String genericparamname = "param" + string.valueof (i + 1), if (!param.containskey (Genericparamname)) {Param.put (ge Nericparamname, Args[parampositions.get (i)]); }} return param; } }
SQL precompiled parameter Settings
Org.apache.ibatis.executor.parameter.DefaultParameterHandler
public void Setparameters (PreparedStatement PS) throws SQLException {errorcontext.instance (). Activity ("setting Parameters "). Object (Mappedstatement.getparametermap (). GetId ()); list<parametermapping> parametermappings = Boundsql.getparametermappings (); if (parametermappings! = null) {MetaObject MetaObject = Parameterobject = = null? NULL:CONFIGURATION.NEWMETAOBJECT (para Meterobject); for (int i = 0; i < parametermappings.size (); i++) {parametermapping parametermapping = Parametermappings.get (i); if ( Parametermapping.getmode ()! = parametermode.out) {Object value; String propertyname = Parametermapping.getproperty (); Propertytokenizer prop = new Propertytokenizer (PropertyName); if (Parameterobject = = null) {value = null;} else if (Typehandlerregistry.hastypehandler (Parameterobject.getclass ())) { value = Parameterobject; } else if (Boundsql.hasadditionalparameter (propertyname)) {value = Boundsql.getadditionalparameter (propertyname);