MyBatis's foreach statement

Source: Internet
Author: User

The main use of foreach is in the build in condition, which can iterate a collection in an SQL statement. The properties of the Foreach element are mainly item,index,collection,open,separator,close. Item represents the alias at which each element of the collection is iterated, and index specifies a name that represents the position at which each iteration occurs during the iteration, and open indicates what the statement begins with.

Separator indicates what symbol is used as a delimiter between each iteration, and close indicates what ends, and the most critical and error-prone thing when using foreach is the collection property, which must be specified, but in different cases The value of this property is not the same, there are 3 main cases:

1. If a single parameter is passed in and the parameter type is a list, the collection property value is List

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

3. If the parameters passed in are multiple, we need to encapsulate them into a map, of course, the single parameter can also be encapsulated as a map, in fact, if you pass in the parameter, in the breast will also wrap it into a map, the map key is the parameter name, So this time the collection property value is the key to the incoming list or array object in its own encapsulated map

1. Type of single parameter list:
<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>

2. Type of single-parameter array arrays:
<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>

MyBatis's foreach statement

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.