MyBatis之foreach实现where..in...查询详解

star2017 1年前 ⋅ 135 阅读

Mybatis 中执行 where...in (xx, xx, xx) 查询时 使用 foreach 遍历参数注意事项。

参数只有一个

参数类型是List

示例:findByIds(List<Long> ids)

只有一个参数,且参数的类型是 List, <foreach> 标签中的 collection 属性要必须指定为 list

<select id="findByIdsMap" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
    from user where id in
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

参数类型是数组

示例:findByIds(Long[] ids)
只有一个参数,且参数的类型是 Array<foreach>标签中的 collection 属性要必须指定为 array

<select id="findByIdsMap" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
    from user where id in
        <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                       #{item}
        </foreach>
</select>

参数有多个时

多参含数组类型

示例:findByIds(String name, Long[] ids)
在传参数时,可以使用 对象 或 Map 对多个参数封装成一个,或可以使用 @param 注解多个参数,这样在 collection 属性必 参数名称 , 示例:

Map<String, Object> params = new HashMap<String, Object>(2);
params.put("name", name);
params.put("ids", ids);
mapper.findByIdsMap(params);
<select id="findByIdsMap" resultMap="BaseResultMap"> 
    select * from user where id in 
    <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
        #{item} 
    </foreach> 
</select>

参数是可变数组

示例:List<user> findByIds(Long... ids)
Mapper 接口参数是个可变数组,使用 where...in 查询的 sql 拼装方法如下:

<select id="findbyIds" resultMap="BaseResultMap">
    select * from user where id in
    <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
        #{item} 
    </foreach> 
</select>

对象中包含集合

集合中是基本类型

对象传参

@Data
@Accessors(chain = true)
public class ApplyApiDetailVO {
    private static final long serialVersionUID = 8208456207930125302L;

    /*List 中是基本类型*/
    private List<Long> idList;
}

Mapper 接口:

@PostMapping("/update")
public ResponseModel update(@RequestBody ApplyApiDetailVO applyApiDetailVO) {
    //...........
}
<foreach item="item" index="index" collection="idList" open="(" close=")" separator="," >
    #{idList[${index}]}
</foreach>

集合中是对象类型

传参对象:

@Data
@Accessors(chain = true)
public class ApplyApiDetailVO {
    private static final long serialVersionUID = 8208456207930125302L;

    /*申请的 API 详情状态*/
    private Integer status;
    /*申请的 API 详情ID集合*/
    private List<ApplyApiDetail> detailList;
}
@Data
@Accessors(chain = true)
public class ApplyApiDetail  {

    /*主键ID*/
    private Long id;
}

Mapper 接口方法:

int updateBatchStatus(ApplyApiDetailVO applyApiDetailVO);

Mapper xml foreach 使用:

<update id="updateBatchStatus" parameterType="applyApiDetailVO">
    update apply_api_detail set status = #{status}
    where id in
    <foreach collection="detailList" item="item" index="index" open="(" close=")" separator=",">
        #{detailList[${index}].id, jdbcType=BIGINT}
    </foreach>
</update>

Controller 使用 ApplyApiDetailVO 对象接收参数,页面传参如下:

Contrller 层接口:

@PostMapping("/updateStatusByApp")
public ResponseModel updateStatusByApp(@RequestBody ApplyApiDetailVO applyApiDetailVO) {
    //...........
}

页面传参:

{
    "status": 0,
    "detailList": [{
        "id":2
    },{
        "id":3
    },{
        "id":4
    },{
        "id":7
    },{
        "id":8
    },{
        "id":9
    }]
}

最终执行的 SQL :

update apply_api_detail set status = 0 where id in ( 2 , 3 , 4 , 7 , 8 , 9 )

分隔符 separator

froeach 的分隔符属性 separator 的值还可以 orand 等字符。

今天写 SQL 就忘记了这一点,遍历时 or 在 SQL 时怎么拼的不行。-- 2019-11-06

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: