Detailed description of the mybatis foreach statement

Source: Internet
Author: User

Detailed description of the mybatis foreach statement

One person adds this to favoritesArticleI want to add 0 comments to my favorites for 113 times three months ago

Foreach is mainly used to construct in conditions. It can iterate a set in SQL statements. Attributes of the foreach element include item, index, collection, open, separator, and close. Item indicates the alias of each element in the set during iteration. Index specifies a name to indicate the position of each iteration during the iteration. Open indicates the start of the statement, separator indicates the separator between each iteration, and close indicates the end of the iteration. When using foreach, the most important and error-prone is the collection attribute, this attribute must be specified, but in different cases, the value of this attribute is different, mainly in the following three cases:

1. If the input is a single parameter and the parameter type is a list, the collection property value is list

2. If the input is a single parameter and the parameter type is an array, the property value of the collection is array.

3. if the input parameters are multiple, We Need To encapsulate them into a map. Of course, a single parameter can also be encapsulated into a map. In fact, if you pass in parameters, in breast, It is encapsulated into a map, and the map key is the parameter name. Therefore, the collection property value is the input list or the key of the array object in the encapsulated map.

The following are examples of the above three cases:Code:

1. single parameter list type:
<Select id = "dynamicforeachtest"
Resulttype = "blog">
Select * from
T_blog where ID in
<Foreach
Collection = "list" Index = "Index" item = "item" open = "(" separator = ","
Close = ")">

# {Item}

</Foreach>

</SELECT>
The value of the above collection is list, and the Mapper corresponding to it is like this

Public list <blog> dynamicforeachtest (list <integer>
IDS );
Test code:
@ Test
Public void
Dynamicforeachtest (){
Sqlsession
Session = util. getsqlsessionfactory (). opensession ();

Blogmapper =
Session. getmapper (blogmapper. Class );

List <integer> IDs = new arraylist <integer> ();

IDs. Add (1 );

IDs. Add (3 );

IDs. Add (6 );
List <blog> blogs =
Blogmapper. dynamicforeachtest (IDS );
For
(Blog: blogs)

System. Out. println (blog );

Session. Close ();
}

 

2. Single Parameter array type:
<Select id = "dynamicforeach2test"
Resulttype = "blog">
Select * from
T_blog where ID in
<Foreach
Collection = "array" Index = "Index" item = "item" open = "(" separator = ","
Close = ")">

# {Item}

</Foreach>

</SELECT>
The above collection is an array, corresponding mapper code:
Public list <blog>
Dynamicforeach2test (INT [] IDs );
Corresponding test code:

@ Test
Public void dynamicforeach2test ()
{
Sqlsession session =
Util. getsqlsessionfactory (). opensession ();

Blogmapper =
Session. getmapper (blogmapper. Class );

Int [] IDs = new int [] {1, 3, 6, 9 };

List <blog> blogs =
Blogmapper. dynamicforeach2test (IDS );

For (blog: blogs)

System. Out. println (blog );

Session. Close ();

}
3. encapsulate parameters as map types.
<Select id = "dynamicforeach3test"
Resulttype = "blog">
Select * from
T_blog where title like "%" # {Title} "%" and ID in

<Foreach collection = "IDS" Index = "Index" item = "item"
Open = "(" separator = "," Close = ")">

# {Item}

</Foreach>

</SELECT>
The value of the preceding collection is IDs, which is the key of the input map parameter, corresponding to the Mapper code:
Public
List <blog> dynamicforeach3test (Map <string, Object>
Params );
Corresponding test code:
@ Test
Public
Void dynamicforeach3test (){
Sqlsession
Session = util. getsqlsessionfactory (). opensession ();

Blogmapper =
Session. getmapper (blogmapper. Class );

Final list <integer> IDs = new
Arraylist <integer> ();

IDs. Add (1 );

IDs. Add (2 );

IDs. Add (3 );

IDs. Add (6 );

IDs. Add (7 );

IDs. Add (9 );
Map <string, Object>
Params = new hashmap <string, Object> ();

Params. Put ("IDS", IDS );

Params. Put ("title", "China ");

List <blog> blogs =
Blogmapper. dynamicforeach3test (Params );

For (blog: blogs)

System. Out. println (blog );

session. Close ();
}

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.