MyBatis use of foreach tags in dynamic sql

Source: Internet
Author: User

The foreach tag is primarily used to build in conditions, and he can iterate over the collection in SQL. As follows:

<delete id= "Deletebatch" >

Delete from the user where ID in

<foreach collection= "Array" item= "id" index= "index" open= "(" close= ")" separator= "," >

#{id}

</foreach>

</delete>

If we say that the parameter is----int[] ids = {1,2,3,4,5}----then the following SQL is printed:

Delete form user where ID in (1,2,3,4,5)

Interpretation:

The value of the Collection:collection property is three list, array, map three, respectively, the corresponding parameter type is: List, array, map collection, I passed the argument is an array, so the value is array

Item: Represents an alias for each element during an iteration

Index: Represents the position of each iteration to (subscript) during the iteration

Open: Prefix

    Close: Suffix

Separator: delimiter that represents what separates each element in the iteration

We can usually use it for bulk deletions, additions, and so on.

MyBatis use of foreach tags in dynamic sql

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.