• 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
  • 相关阅读:
    js数据什么时候是null什么时候是undefined
    持续集成部署-k8s-数据持久化-NFS安装与使用
    开发者API管理神器Eolink,比postman好用
    计算机毕业设计Java校园兼职网站(源码+系统+mysql数据库+Lw文档)
    UX设计VSUI设计
    YOLOV7训练TT100K交通标识符数据集
    yolov5量化注意事项(二)
    LLM 系列 | 21 : Code Llama实战(上篇) : 模型简介与评测
    NFT 智能合约实战-快速开始(1)NFT发展历史 | NFT合约标准(ERC-721、ERC-1155和ERC-998)介绍
    Python高级篇(08):生成器
  • 原文地址:https://blog.csdn.net/weixin_45594025/article/details/137276589