如何传入字符串参数,分割并遍历?

如前台传入字符串参数

1
String str = "a,b,c,d,e,f"

现需将此参数作为查询语句的参数

1
Select * from news where id in (${id})

使用该语句查询正常返回结果,但势必产生sql注入漏洞

如修改为:

1
Select * from news where id in (#{id})

程序报错

正确方法如下

1
2
id in
<foreach collection="str.split(',')"  item="item" index="index" open="(" separator="," close=")">#{item}</foreach>