Source interpretation MyBatis list in query implementation considerations

Source: Internet
Author: User

In SQL development, dynamically building in-set conditional queries is a common use, with the foreach feature in MyBatis, which allows you to specify a collection that declares collection items and index variables that can be used within an element body. It also allows you to specify open and closed strings and place delimiters between iterations. This element is very intelligent and does not accidentally append extra separators. 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>

However, due to the official document on the use of this piece, the description of the relatively short, the details are also ignored (may be open source project documentation has always been the problem of it), but also use a lot of students in the use of problems encountered. Especially in the Foreach function, what is the collection property for, and what are the considerations. Due to incomplete documentation, this block can only be analyzed by source code to analyze the requirements of each property.

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

The following is the beginning of the analysis of the source code (note using MyBatis 3.0. Version 5)

Find the MyBatis to perform SQL configuration parsing first

Mappermethod.java class public Object execute (object[] args) The method is the entry for execution.

For in-set queries, the application is the Selectforlist or Selctformap method.

But regardless of which method is called, the original JDK passed in the parameter object[] type, through the GetParam method into an Object, what is this method to do? Analyze the source code as follows:

Won the red two places, very surprised to find that a parameter and a number of parameters of the processing method is different (after many students encounter problems, there is a large part from this place). If the number of parameters is greater than one, it will be encapsulated as a map, the key value if the MyBatis param annotation is used, the key value will be used, otherwise the default uniform use of the data ordinal, starting from 1. This problem is noted first, continue to analyze the code, and then if the selectforlist operation (the other operation on the application of the corresponding method), will call the Defaultsqlsession public List selectlist (String statement, Object parameter, Rowbounds rowbounds) method

Another discovery, see the source code is as follows:

The red part, the parameter is again encapsulated, we look at the code

It's clear now that if the parameter type is list, it must be specified in Collecion as list, and if it is a data group, it must be specified as an array in the collection property.

Now the problem is clear, if it is a parameter, the value of collection depends on your parameter type.

If you have multiple values, it is the beginning of a number unless you specify it with annotation param, so it is useless to specify what value in collection. is the debug display result.

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

In using this feature you need to pay special attention to the following rules:
Findbyids (list<long> IDs)
1.a if the type of the parameter is list, then when used, the collection property must be specified as a list
<select id= "Findbyidsmap" resultmap= "Baseresultmap" >
Select
<include refid= "Base_column_list"/>
From Jria where ID in

Open= "(" separator= "," close= ")" >
#{item}
</foreach>

Findbyids (long[] IDs)
1.b if the type of the parameter is an array, the collection property must be specified as an array when used
<select id= "Findbyidsmap" resultmap= "Baseresultmap" >
Select
<include refid= "Base_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 mode, 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= "Base_column_list"/>
From Jria where ID in
<foreach item= "item" index= "index" collection= "IDs "
open= "(" separator= "," close= ")" >
#{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 using in query is as follows:
<select id= "Findbyids" resultmap= "Baseresultmap" >
Select
<include refid= "Base_column_list"/>
From Jria where ID in
<foreach item= "item" index= "index" collection= "array"
open= "(" separator= "," close= ")" >
#{item}
</foreach>

</select>

Source interpretation MyBatis list in query implementation considerations

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.