02. 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;
}

2.1 批量插入

Repository 接口中定义方法:

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

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>

2.2 批量编辑

Repository 接口中定义方法:

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

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>

2.3 批量查询

Repository 接口中定义方法:

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

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>

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

Repository 接口中定义方法:

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

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>

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);

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>
  • 18
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值