A parameter and the parameter value of the processing mybatis
1, a single parameter
-
mybatis no treatment
-
Value ways:
# {Parameter name / arbitrary name}
<!-- Employee getEmpById(Integer id); --> <select id="getEmpById" resultType="com.mxc.bean.Employee"> select * from employee where id=#{id} </select>
2, a plurality of parameters
-
mybatis will be packaged as a plurality of parameters to automatically map
key: param1, ..., paramN, may be 0, ..., N-1 (i.e., the parameter index, N is the number of parameters)
value: Parameter Value
-
Value ways:
The above-described key} {#
<!-- Employee getEmpByIdAndLastName(Integer id, String lastName); --> <select id="getEmpByIdAndLastName" resultType="com.mxc.bean.Employee"> select * from employee where id=#{param1} and last_name=#{param2} </select>
Assigning clear key for multiple parameters
-
@Param
Annotation key may specify an explicit parameter, the parameter values for easy access in the sql -
Value ways:
# {@ Param annotation specified key}
<!-- Employee getEmpByIdAndLastName(@Param("id")Integer id, @Param("lastName")String lastName); --> <select id="getEmpByIdAndLastName" resultType="com.mxc.bean.Employee"> select * from employee where id=#{id} and last_name=#{lastName} </select>
Long encapsulates parameters map, easy to take the value of the parameter used to specify the use of the package key @Param
3, the parameter is a POJO
-
If the plurality of parameters is exactly the POJO attribute value may be directly into a POJO
-
Value ways:
Attribute name} {#
<!-- Employee getEmpByIdAndLastName(Employee emp); --> <select id="getEmpByIdAndLastName" resultType="com.mxc.bean.Employee"> select * from employee where id=#{id} and last_name=#{lastName} </select>
4, the argument is a Map
-
If the parameter is not one of the plurality of POJO properties, it may be packaged as a Map
-
Value ways:
#{Map的key}
<!-- Employee getEmpByIdAndLastName(Map<String, Object> map); map: Map<String, Object> map = new HashMap<>(); map.put("id", 1); map.put("lastName", "mxc"); --> <select id="getEmpByIdAndLastName" resultType="com.mxc.bean.Employee"> select * from employee where id=#{id} and last_name=#{lastName} </select>
Small scale chopper
Employee getEmp(@Param("id")Integer id,String lastName);
// 取值:id=>#{id/param1},lastName=>#{param2}
Employee getEmp(Integer id,@Param("e")Employee emp);
// 取值:id=>#{param1},lastName => #{param2.lastName/e.lastName}
$ # {} {} And the similarities and differences of
the same:
Value can be obtained in the map object property value or pojo
different:
# {}: Is pre-compiled form, set the parameter to the sql statement, sql injection can be prevented
$ {}: value taken directly assembled in the sql statement, there will be safety problems
usage scene
In most cases, taking the value of the parameter using # {}. {} $ Can be used in places not support native JDBC placeholders. As by year table query: select * from $ {year} _salary;
Second, the source processing parameters to achieve mybatis
1, an internal data structure
Package com.spring.test.service.mybatis; Import org.apache.ibatis.builder.ParameterExpression; / ** * * / public class paramHandler { public static void main (String [] args) { paramsTokenHandler (); } / * * * method name: list <the User> queryList (String name, RowBounds RowBounds, int Age, @Param ( "address") String addresss); * internal parameter mapping: * MethodSignature.SortedMap <Integer, String> params * parameter list subscript -> parameter name (if it is @Param, it shows notes of value, otherwise the current number of parameters in addition to ranking RowBounds, ResultHandler for the parameter list) * 0-> 0 * 2-> 1 * 3-> address * * MethodSignature.convertArgsToSqlCommandParam (Object [] args) Returns: paramMap * * paramMap internal memory as follows: * the parameter names -> parameters subscript * 0-> name value * 1-> age values * Address-> addresss of values * * param1-> name value * param2-> age value value * param3-> addresss of * * / public static void paramsToParamMap () { } / ** * examples: # {checkedTime, jdbcType = BIGINT , typeHandler = com. } meituan.payment.fundsgateway.core.model.handler.DateForLongTypeHandler * / public static void paramsTokenHandler () { String token="checkedTime,jdbcType=BIGINT,typeHandler=com.meituan.payment.fundsgateway.core.model.handler.DateForLongTypeHandler"; ParameterExpression parameterExpression=new ParameterExpression(token); String name=parameterExpression.get("property"); System.out.println(name); String jdbcType=parameterExpression.get("jdbcType"); System.out.println(jdbcType); String typeHandler=parameterExpression.get("typeHandler"); System.out.println(typeHandler); } }
2, the special data structures and algorithms involved
org.apache.ibatis.scripting.defaults.DefaultParameterHandler
=> Sql resolved by looping through ParameterMapping obtain a specified value from the argument list, provided to the sql statement? Placeholders.
org.apache.ibatis.reflection.MetaObject
=> A method of packaging into a parameter list MetaObject, provides a method of obtaining uniform parameter value
org.apache.ibatis.reflection.property.PropertyTokenizer
=> # {PropertyName} placeholder in propertyName MetaObject acquired according to sql statement sql statement? Value.
org.apache.ibatis.builder.SqlSourceBuilder
org.apache.ibatis.builder.ParameterMappingTokenHandler
org.apache.ibatis.parsing.GenericTokenParser
org.apache.ibatis.builder.ParameterExpression
=> Sql statement in the {} # replace? , And parse the List <ParameterMapping>