这里记录一个关于 Mybatis 中 if 标签的知识点
在使用<if test=""> </if>
时, 当 test 中 的对比条件为一个字符时, 有可能会发生类型转换异常 java.lang.NumberFormatException
说在前面 : 我传入 Mybatis 的参数是 String 类型 @Param("workshift")String workshift
例如 :
<if test="workshift !=null and workshift != '' and workshift != 'a'">
AND
workshift=#{workshift}
</if>
其中 ‘a’ 可能会被解析为字符, 从而发生以上异常
解决方法 :
可以在将 ‘a’ 转换为字符串 即给条件加上toString()
<if test="workshift !=null and workshift != '' and workshift != 'a'.toString()">
AND
workshift=#{workshift}
</if>
或将条件换为多字符(前提条件允许的话)
<if test="workshift !=null and workshift != '' and workshift != 'aaa'">
AND
workshift=#{workshift}
</if>
参考博客 : https://blog.csdn.net/qq_44988088/article/details/108770179