mybatis foreach操作map
时间: 2023-10-17 11:01:55 浏览: 79
Mybatis操作实例
MyBatis provides a convenient way to iterate over a collection in a `foreach` loop using a `Map`. Here's an example of how you can achieve this:
First, define your input map in your parameter object or as a method parameter in your MyBatis mapper interface. For example, let's assume you have a map with keys "ids" and "names":
```java
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("ids", Arrays.asList(1, 2, 3)); // list of IDs
inputMap.put("names", Arrays.asList("John", "Jane", "Alice")); // list of names
```
In your MyBatis XML mapper file, you can use the `foreach` tag to iterate over the map values. Here's an example of how to use it in a select statement:
```xml
<select id="selectUsers" resultType="User">
SELECT * FROM users
WHERE id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
AND name IN
<foreach collection="names" item="name" open="(" separator="," close=")">
阅读全文