mybatis查询时入参为Map<String, Set<String>> 怎么遍历map的key 和 value
时间: 2024-10-28 22:10:03 浏览: 71
当你在MyBatis中查询并且参数是一个Map<String, Set<String>>
,你可以通过设置SQL语句中的占位符${}
来动态绑定数据。例如,假设你的Mapper接口有如下的方法:
List<YourEntity> queryWithMap(@Param("params") Map<String, Set<String>> params);
然后在对应的Sql映射文件(.xml
)中,你可以这样编写动态SQL来遍历Map的键值对:
<select id="queryWithMap" resultType=YourEntity>
SELECT * FROM your_table
WHERE key IN <foreach item="itemKey" index="index" collection="params.keySet()" open="(" separator="," close=")">
#{itemKey}
</foreach>
AND value IN <foreach item="itemValue" index="index" collection="params.values()" open="(" separator="," close=")">
<foreach item="innerItem" index="innerIndex" collection="itemValue">
#{innerItem}
</foreach>
</foreach>
</select>
这里使用了两次foreach
标签,外层遍历键(keySet),内层遍历每个值集合(values)。这样,MyBatis会将params
中的每一个键及其对应的值集合作为查询条件。
相关推荐


















