mybatisplus遍历集合查询sql
时间: 2023-11-09 13:18:14 浏览: 223
支持连表查询的mybatis-plus
5星 · 资源好评率100%
mybatisplus遍历集合查询sql可以使用以下两种方式来实现:
方式一:将条件语句写在<foreach>标签的open和close属性中,将遍历集合查询的条件放在<foreach>标签的body中
```java
<select id="listAnimals" resultType="com.zhang.entity.Animal">
SELECT * FROM animal WHERE 1=1
<if test="ids != null and ids.size() > 0">
and `id` in
<foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
```
方式二:将条件语句嵌入到左边的圆括号中,在<foreach>标签中直接写入遍历集合查询的条件
```java
<select id="listAnimals" resultType="com.zhang.entity.Animal">
SELECT * FROM animal WHERE 1=1
<if test="ids != null and ids.size() > 0">
<foreach collection="ids" item="id" index="index" open="and `id` in (" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
```
以上两种方式都可以实现mybatisplus遍历集合查询sql的功能,具体使用哪种方式取决于个人偏好和实际业务需求。<span class="em">1</span><span class="em">2</span>
#### 引用[.reference_title]
- *1* *2* [mybatis动态sql,遍历集合查询](https://blog.csdn.net/m0_59259076/article/details/122697871)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文