MyBatis-Dynamic SQL-foreach

Table of contents

The `` tag has the following common attributes:

summary


<froeach>

The `<foreach>` tag has the following common attributes:

  1. `collection`: Specifies the parameter name of the collection or array to iterate ( the object to traverse ).
  2. `item`: Specifies the alias of each element in the iteration process ( traversed elements ).
  3. `index`: Specifies the index value of each element in the iteration process.
  4. `open`: A string specifying the start of the loop ( the SQL fragment concatenated before the traversal starts ).
  5. `close`: Specifies the string at the end of the loop ( the SQL fragment spliced ​​after the end of the traversal ).
  6. `separator`: Specifies the separator between each element.
  7. `jdbcType`: Specifies to convert each element to the specified JDBC type.
  8. `javaType`: Specifies to convert each element to the specified Java type.
  9. `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

Guess you like

Origin blog.csdn.net/weixin_64939936/article/details/132113963