<if test=" name!=null && name =='1' "><if/>
java中的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>
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态