在过滤条件中,某个条件有多个值情况。如下

当出现这个情况时,做mybaitis查询要怎么写方便
方法肯定多种,这里体现我常用的一种。
1.首先入参:
<1>.可以是 String 以‘,’隔开:"123,456,6667"。
<2>.可以是String[] 数组。
<3>.可以是一个List。
2.不管以什么形式入参,最终都要构造成一个List集合。
3.Mybatis写法
<if test="employeeNameList != null and employeeNameList != '' and employeeNameList.size() >0">
AND su.name in
<foreach collection="employeeNameList" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
注解:employeeNameList :是刚才所说的最终构造成的一个集合,item:是集合里每一个值
其他的脚本该怎么写就怎么写,这里只是提供一个条件多值情况
另:在Mybatis 中字符串相等情况,
例如:
错误实例:
<if test="priceChange != null and priceChange != '' ">
<if test="priceChange == '1'">
and fap.id is not null
</if>
<if test="priceChange == '0'">
and fap.id is null
</if>
</if>
当 前端入参 priceChange = ‘1’ 时,在if 过滤中是无法进去的,
应改成:
<if test="priceChange != null and priceChange != ''">
<if test="priceChange == '1'.toString()">
and fap.id is not null
</if>
<if test="priceChange == '0'.toString()">
and fap.id is null
</if>
</if>
这样才能进入条件判断。
本文详细介绍在MyBatis中如何高效处理具有多个值的过滤条件,包括使用String、String[]数组或List作为参数的不同方式,并提供实际代码示例。同时,文章纠正了在字符串相等判断中常见的错误做法。
1857

被折叠的 条评论
为什么被折叠?



