文章目录
- 1. foreach 标签
- 2. MyBatis<foreach>标签的使用
- 2.1 批量插入
- 2.2 批量编辑
- 2.3 批量查询
- 2.4 使用 foreach 遍历 map
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 中 实现如下:
<insert id="batchInsert" parameterType="">
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 中 实现如下:
<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 中 实现如下:
<select id="batchSelect" resultMap="DAOMap">
select * from ${tableName}
where 1 = 1
<if test="planIds != null and () > 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 中 实现如下:
<select id="batchSelect" resultMap="PlanBaselineStateDAOMap">
select * from ${tableName}
where 1 = 1
<if test="planIds != null and planIds != ''">
and `plan_id` in
<foreach collection="(',')" 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 中 实现如下:
<insert id="batchInsert" parameterType="">
insert into ${tableName} (`workspace_key`, `plan_id`)
values
<foreach collection="batchInsertMap" index="key" item="value" separator=",">
(#{key}, #{value})
</foreach>
</insert>