在项目开发中经常会出现一个查询字段包含多个查询内容的情况,在SQL中使用IN实现:
SELECT * FROM user WHERE name IN ('张三','李四');
在mybatis,使用foreach实现in的功能:
<where> <if test="isAudit != null"> and v.isAudit in <foreach collection="isAudit" index="index" item="isAuditw" open="(" separator="," close=")"> #{isAuditw} </foreach> </if> </where>
collection表示传入的值,一般为list类型,item表示命名,open表示起始字符,close表示结束字符,separator表示分隔符。