How to use the “in” parameter in MyBatis?
In MyBatis, the
Here is an example demonstrating how to use the
Firstly, define a
<select id="selectUsers" resultType="User">
SELECT *
FROM users
WHERE id IN
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
</select>
Next, in the corresponding Mapper interface, define a method that corresponds to the
List<User> selectUsers(List<Integer> userIds);
Finally, when using the Mapper interface, pass a parameter containing the list of IDs that need to be queried.
List<Integer> userIds = Arrays.asList(1, 2, 3);
List<User> users = userMapper.selectUsers(userIds);
This allows us to pass parameters for the IN condition using the