在我们的业务场景中,前段经常传过来一些用逗号“,”隔开的字符串,我们怎么在mybatis中将字符串分割后再用in查询呢?
废话少说,上代码
<!--查询名字为某某某的用户信息-->
<select id="getUser" parameterType="java.lang.String" resultType="java.util.Map">
select id,name,age
from user
where 1==1
<if test="names != null and names != ''">
<!--查看是否查询多个人,有逗号是多个人-->
<if test="stringParam.indexOf(',') != -1">
and name in
<foreach item="item" index="index" collection="names.split(',')" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="names.indexOf(',') == -1">
and name=#{names}
</if>
</if>
</select>
好了,这样就完成了简单的查询。