• MyBatis<foreach>标签的用法


    1. foreach 标签

    foreach 可以在 SQL 语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。

    • collection 指定要遍历的集合。表示传入过来的参数名。该属性是必须指定的,要做 foreach 的对象。
    • item 表示本次迭代获取的元素,若 collection 为 List、Set 或者数组,则表示其中的元素;若collection 为 map,则 item 代表 key-value 的 value,该参数为必选;
    • index 索引,index 指定一个名字,用于表示在迭代过程中,每次迭代到的位置。遍历 list 的时候 index 就是索引,遍历 map 的时候 index 表示的就是 map 的 key,item 就是 map 的 Value。
    • open 表示该语句以什么开始,该参数为可选项(Mybatis 会将该字符拼接到整体的 sql 语句之前,并且只拼接一次);
    • separator 表示在每次进行迭代之间以什么符号作为分隔符;
    • close 表示以什么结束,该参数为可选项。

    2. MyBatis<foreach>标签的使用

    假设我们有一个 BatchDTO 的实体类

    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Builder(toBuilder = true)
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public class BatchDTO {
        private String workspaceKey;
        private String planId;
        private String baselineState;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.1 批量插入

    Repository 接口中定义方法:

    boolean batchInsert(@Param("list") List<BatchDTO> batchDTOList, @Param("tableName") String tableName);
    
    • 1

    Mybatis 中 mapper.xml 实现如下:

        <insert id="batchInsert" parameterType="java.util.List">
            insert into ${tableName} (`workspace_key`, `plan_id`, baseline_state)
            values
            <foreach collection="list" item="item" separator=",">
                (#{item.workspaceKey}, #{item.planId}, #{item.baselineState})
            </foreach>
        </insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2 批量编辑

    Repository 接口中定义方法:

    boolean batchUpdate(@Param("planIds") List<BatchDTO> batchDTOList, @Param("tableName") String tableName);
    
    • 1

    Mybatis 中 mapper.xml 实现如下:

        <update id="batchUpdate">
            update ${tableName}
            set `workspace_key` = "vvv"
            where `plan_id` in
                <foreach collection="planIds" item="item" index="index" open="(" separator="," close=")">
                    #{item.planId}
                </foreach>
        </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.3 批量查询

    Repository 接口中定义方法:

    List<DAO> batchSelect(@Param("planIds") List<String> planIds, @Param("tableName") String tableName);
    
    • 1

    Mybatis 中 mapper.xml 实现如下:

        <select id="batchSelect" resultMap="DAOMap">
            select * from ${tableName}
            where 1 = 1
            <if test="planIds != null and planIds.size() > 0">
                and `plan_id` in
                <foreach collection="planIds" open="(" item="planId" close=")" separator=",">
                    #{planId}
                </foreach>
            </if>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    如果入参类型是 List 类型,可以直接以字符串类型作为入参,而不用传入一个 List。具体实现如下。

    Repository 接口中定义方法:

    List<DAO> batchSelect(@Param("planIds") String planIds, @Param("tableName") String tableName);
    
    • 1

    Mybatis 中 mapper.xml 实现如下:

        <select id="batchSelect" resultMap="PlanBaselineStateDAOMap">
            select * from ${tableName}
            where 1 = 1
            <if test="planIds != null and planIds != ''">
                and `plan_id` in
                <foreach collection="planIds.split(',')" open="(" item="planId" close=")" separator=",">
                    #{planId}
                </foreach>
            </if>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.4 使用 foreach 遍历 map

    这里举例使用 foreach 遍历 map。使用 foreach 遍历 map 时,foreach 标签中的参数 index 表示的就是 map 的 key,item 就是 map 的 Value。

    Repository 接口中定义方法:

    boolean batchInsert(@Param("batchInsertMap") Map<String, String> batchInsertMap, @Param("tableName") String tableName);
    
    • 1

    Mybatis 中 mapper.xml 实现如下:

        <insert id="batchInsert" parameterType="java.util.Map">
            insert into ${tableName} (`workspace_key`, `plan_id`)
            values
            <foreach collection="batchInsertMap" index="key" item="value" separator=",">
                (#{key}, #{value})
            </foreach>
        </insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    深圳锐科达SV-X7 sip话机与海康威视摄像头联动设置方法
    黑马JVM总结(二十七)
    C++学习笔记13 - 浅拷贝和深拷贝
    iptables DNAT和de-DNAT
    ConcurrentHashMap-1
    SpringBoot+Vue实现前后端分离的乒乓球馆预约管理系统
    MongoDB基础学习
    海外公司尽职调查 境外投资尽职调查 海外并购尽职调查
    Oracle数据库:oracle组函数,聚合函数,多行函数,avg,sum,min,max,count,group by,having
    Linux - 基本开发工具
  • 原文地址:https://blog.csdn.net/weixin_45594025/article/details/137276589