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