<foreach> tags have the function of looping, which can be used to generate regular SQL statements, and the main attributes are:
Item: Represents the alias at which each element of the collection iterates
Index: Indicates where each iteration is to during the iteration
Open: Indicates what the statement has started
Separator: Indicates what symbol is used to separate each iteration
Close: Indicates what the statement has ended
Collection: Variables that need to be iterated
<SelectID= "Selectpostin"Resulttype= "Domain.blog.Post">SELECT * from POST P WHERE ID in<foreachItem= "Item"Index= "Index"Collection= "List"Open="("Separator=","Close=")">#{item}</foreach></Select>
The functionality of the Foreach element is very powerful, allowing you to specify a collection that declares the collection items and index variables that can be used within the body of the element. It also allows you to specify the opening and closing of a matching string and place the delimiter in the middle of the iteration. This element is very intelligent, so it does not accidentally append extra separators.
Note that you can pass a List instance or array as a parameter object to MyBatis, and when you do, MyBatis will automatically wrap it in a Map with the name key. The list instance will be "list" as the key, and the array instance key will be "array".
To this we have completed a discussion of the XML configuration file and the XML mapping file. The next section explores the Java API in detail so that you can derive maximum benefit from the mappings you have created.
Mybatis <foreach> Tags