Source code interpretation MyBatis list in query implementation of considerations

Source: Internet
Author: User
Tags documentation

In the SQL development process, dynamic build in Set condition query is a more common usage, in MyBatis provides a foreach function, which is more powerful, it allows you to specify a set, declare the collection items and index variables, they can be used in the element body. It also allows you to specify open and closed strings, and to place delimiters between iterations. This element is intelligent and does not accidentally append extra delimiters. Here is a demo example:

<select id= "Findbyidsmap" resultmap= "Baseresultmap" >
Select
<include refid= "Base_column_list"/>
From  Jria where ID in
Open= "(" separator= "," close= ")" >
#{item}
</foreach>

But because of the official document on the use of this piece, the description is relatively short, the details are also ignored (probably the open source project documentation has always been the problem), but also use a lot of students in the use of the problem encountered. In particular, this function of foreach, what to do with the collection property, what to note. Due to incomplete documentation, this piece can only be analyzed by the source code analysis of the relevant requirements of each attribute.

The purpose of the collection property is to receive an array of inputs or a list interface implementation. But for its name requirements, MyBatis in the implementation is still a bit difficult to understand, so need to pay special attention to this point.

The following begins the analysis of the source code (notes using the MyBatis 3.0.5 version)

First find the portal that MyBatis performs SQL configuration resolution

The Mappermethod.java class is Public Object execute (object[] args) The method is the entry into the execution. For in-set queries, the application is the Selectforlist or Selctformap method.

But regardless of which method is invoked, the parameter object[] type passed in the original JDK is converted to an Object by the GetParam method, what is this method doing? Analysis of the source code is as follows:

The image of the red in the two places, very surprised to find that a parameter and multiple parameters of the treatment is different (follow-up many students encountered problems, a large part of this place). If the number of parameters is greater than one, it will be encapsulated into a map, the key value if the use of mybatis param annotation, the key value will be used, or the default uniform use of data ordinal, starting from 1. The problem is noted first, continue parsing the code, and then if the Selectforlist action (the other action is applied to the appropriate method) , will call Defaultsqlsession public List selectlist (String Statement, Object parameter, Rowbounds rowbounds) method

Another discovery, see source code as follows:

On the red part of the icon, to the parameters of another package, we look at the code


Now it's a bit clear that if the parameter type is list, you must specify the list in Collecion and, if it is a data group, you must specify the array in the collection property.

Now the question is clearer, and if it is a parameter, the value of the collection depends on your parameter type.

If it is more than one value, the value specified in collection is useless unless specified with the annotation param, otherwise it is the beginning of the number. The following figure shows the results of the debug display.

For the results of the analysis above, the following gives a solution to use, hope for everyone to help.

In using this feature it is necessary to pay special attention to the following rules:
Findbyids (list<long> IDs)
1.a if the type of the parameter is list, the collection property must be specified as list when used
<select id= "Findbyidsmap" resultmap= "Baseresultmap" >
Select
<include refid= "Base_column_list"/>
From Jria where ID in
 <foreach item= "item" index= "index" collection= "list" 
  Open = "(" separator= "," close= ")" > #{item} </foreach> 
 
Findbyids (long[] IDs)
1.b if the type of the argument is array, the collection property must be specified as array when used
<select id= "Findbyidsmap" resultmap= "Baseresultmap" > select <include refid= "Bas E_column_list "/> from Jria where ID in<foreach item= "item" index= "index" collection= "array" open= "(" separator= "," close= ") "> #{item} </foreach> </select>
2. When there are multiple parameters for the query, such as Findbyids (String name, long[] IDs)
This situation requires special attention, when passing parameters, be sure to use the map method, so that the collection property can specify the name
Here is an example
map<string, object> params = new hashmap<string, object> (2);
Params.put ("name", name);
Params.put ("IDs", IDS);
Mapper.findbyidsmap (params);
<select id= "Findbyidsmap" resultmap= "Baseresultmap" > select <include refid= "Bas E_column_list "/> from Jria where ID in<foreach item= "item" index= "index" collection= "IDs" open= "(" separator= "," close= ")" &                        Gt #{item} </foreach> </select>
The complete example is as follows:
For example, there is a query function, the Mapper interface file defines the following methods:
List<jria> findbyids (Long ... IDs);
The SQL assembly method used in query is as follows:
<select id= "Findbyids" resultmap= "Baseresultmap" > select <include refid= "Base_c" Olumn_list "/> from Jria where ID in<foreach item= "item" index= "index" collection= "array" open= "(" separator= "," close= ") "> #{item} </foreach> </select>

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.