<foreach>传参list<string>
时间: 2023-08-17 08:15:59 浏览: 67
在使用<foreach>标签传递List<String>参数时,可以使用以下两种方式。
第一种方式是在Mapper接口的方法参数前面添加@Param注解,并将List<String>参数命名为list。例如:
引用\[1\]中的示例代码:
```java
public List<Map<String, Object>> dynamicForeachTest(@Param("list") List<String> ids);
```
引用\[1\]中的示例代码:
```xml
<select id="dynamicForeachTest" resultType="java.util.Map">
select * from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
```
第二种方式是直接将List<String>参数命名为ids,不需要添加@Param注解。例如:
引用\[2\]中的示例代码:
```java
public List<Map<String, Object>> dynamicForeach3Test(List<String> ids, @Param("contractId") String contractId);
```
引用\[2\]中的示例代码:
```xml
<select id="dynamicForeach3Test" resultType="java.util.Map">
select * from xxx where cid=#{contractId} and fID in
<foreach collection="ids" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
```
无论使用哪种方式,都可以通过<foreach>标签遍历List<String>参数中的每个元素,并将其作为SQL语句的参数进行查询。
#### 引用[.reference_title]
- *1* *2* *3* [MyBatis中in <foreach>的使用](https://blog.csdn.net/xujunkai66/article/details/81413818)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)