Table of contents
The `` tag has the following common attributes:
<froeach>
The `<foreach>` tag has the following common attributes:
- `collection`: Specifies the parameter name of the collection or array to iterate ( the object to traverse ).
- `item`: Specifies the alias of each element in the iteration process ( traversed elements ).
- `index`: Specifies the index value of each element in the iteration process.
- `open`: A string specifying the start of the loop ( the SQL fragment concatenated before the traversal starts ).
- `close`: Specifies the string at the end of the loop ( the SQL fragment spliced after the end of the traversal ).
- `separator`: Specifies the separator between each element.
- `jdbcType`: Specifies to convert each element to the specified JDBC type.
- `javaType`: Specifies to convert each element to the specified Java type.
- `typeHandler`: Specifies to convert each element to the specified type handler.
The specific implementation of the SQL statement is as follows
SQL statements executed in the XML mapping file
<!--批量删除员工信息-->
<delete id="DeleteByIDs">
delete
from emp
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
The method in the Mapper interface is
public void DeleteByIDs(List<Integer> ids);
code in test class
@Test
public void TestDeleteByIDS() {
List<Integer> ids = Arrays.asList(1, 2, 3);
empMapper.DeleteByIDs(ids);
}
summary
Attributes in the <foreach> tag
- collection: collection name
- item: the name of the element traversed by the collection
- separator: the separator used for each traversal
- open: the segment spliced before the traversal starts
- close: the spliced fragments after traversal