判断等于一个字符串
<if test=" name!=null && name =='1' "><if/>
这样写会出现后面的 name =='1'失效问题。 很多人会踩的坑
因为mybatis映射文件,是使用的ognl表达式,所以在判断字符串变量是否是字符串的时候 会把'1'解析为字符,java是强类型语言,所以不能这样写
<!--把这个转换成 单引号。这样就解决了--> <if test=' name!=null && name == "1" '><if/> <!-- 推荐使用 --> <if test=" name!=null && name == '1'.toString() "><if/>
当判断条件字符串不包含字母,需要.toString()
<if test="input eq '1'.toString()"> and user_id = #{userId} </if> <if test="input eq '0'.toString()"> and user_id != #{userId} </if>
当判断条件字符串包含字母,不需要.toString()
<if test="name eq 'str'"> and user_id = #{userId} </if> <if test="name eq 'str'"> and user_id != #{userId} </if>