我有这种结构的SQL查询:
......
......
where a.id=#{x} AND b.id!=#{x}
......
现在我想更改此查询以接受x的值列表。我应该如何为此编写foreach循环?
答案 0 :(得分:2)
我会将您的WHERE更改为表单
SELECT ... FROM ... WHERE a.id IN (...x...) AND b.id NOT IN (...x...)
然后,您的注释或XML将包含:
SELECT ...
FROM ...
WHERE a.id IN <foreach item="item" index="index" collection="x"
open="(" separator="," close=")">
#{item}
</foreach>
AND b.id NOT IN <foreach item="item" index="index" collection="x"
open="(" separator="," close=")">
#{item}
</foreach>
这将产生更高效的SQL。