mybatis foreach 字符串数组
时间: 2023-08-25 20:17:54 浏览: 172
MyBatis provides a "foreach" tag that allows you to iterate over a collection, including an array of strings. Here's an example of how you can use the "foreach" tag with a string array in MyBatis:
```xml
<select id="getUserByIds" resultType="User">
SELECT * FROM users
WHERE id IN
<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
#{item}
</foreach>
</select>
```
In this example, "getUserByIds" is the ID of the SQL query, and "User" is the result type of the query. The "ids" attribute refers to the string array parameter passed to the query. The "open" attribute specifies the opening string before the iteration, the "separator" attribute specifies the separator between each item, and the "close" attribute specifies the closing string after the iteration.
You can then use this query in your Java code and pass a string array called "ids" to the MyBatis mapper method:
```java
String[] ids = { "1", "2", "3" };
List<User> users = sqlSession.selectList("getUserByIds", ids);
```
This will execute the SQL query and retrieve the users with the specified IDs from the database.
阅读全文