1 起因
在实际业务开发中, 例如商城搜索, 我们肯定会遇到前端传递数组参数的情况
例如我在选机械键盘, 那么我会从akko和NIZ两个品牌中挑选, 也可能只从GANSS一个品牌中挑选
在这种情况下, 当前端传递GANSS时, 参数会是字符串(枚举), 而传递akko和NIZ时, 会是数组
针对这种情况, Mybatis中的where条件改如何编写呢?
2 解决方案
在if中嵌套使用choose即可, 直接上代码:
<where>
1=1 and t.is_delete = 0
<if test='parameters.types != null'>
<choose>
<when test="parameters.types instanceof String">
and t.type = #{parameters.types}
</when>
<otherwise>
and t.type in
<foreach collection="parameters.types" item="type" index="index" open="(" separator="," close=")">
#{type}
</foreach>
</otherwise>
</choose>
</if>
</where>
Java中是Instanceof, Mybatis中是instanceof, i要小写