mybatis if標簽判斷不生效的解決方法
實際需求
<if test='computationRule == ’1’'> FROM app_sz_bbb a</if><if test='computationRule == ’2’'> FROM app_ccc a</if>
這種情況不生效,
原因:mybatis是用OGNL表達式來解析的,在OGNL的表達式中,’0’會被解析成字符,java是強類型的,char 和 一個string 會導致不等,所以if標簽中的sql不會被解析。
先說怎么解決
三種:
加 .toString()
<if test='computationRule == ’1’.toString()'> FROM app_sz_bbb a</if><if test='computationRule == ’2’.toString()'> FROM app_ccc a</if>
choose when 標簽代替
<choose> <when test='computationRule == ’1’'> FROM app_sz_bbb a </when> <otherwise> FROM app_sz_bbb a </otherwise> </choose>
單引號 換成雙引號
<if test=’computationRule == '1'’> FROM app_sz_bbb a</if><if test=’computationRule == '2'’> FROM app_ccc a</if>MyBatis 中if 標簽 判斷字符串不生效
異常sql 的mapper 文件:
<if test='isBound != null and isBound !=’’ and isBound == ’1’'> and box_sid is not null </if><if test='isBound != null and isBound !=’’ and isBound == ’2’'> and box_sid is null </if>
正確sql 的mapper 文件
<if test='isBound != null and isBound !=’’ and isBound == ’1’.toString()'> and box_sid is not null </if><if test='isBound != null and isBound !=’’ and isBound == ’2’.toString()'> and box_sid is null </if>
到此這篇關于mybatis if標簽判斷不生效的解決方法的文章就介紹到這了,更多相關mybatis if標簽判斷不生效內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. oracle觸發器介紹2. 使用mysql記錄從url返回的http GET請求數據操作3. DB2中多種常用功能的解決方法(1)4. MYSQL數據庫存文本轉存數據庫問題5. mysql存儲過程多層游標循環嵌套的寫法分享6. Mybatis傳入List實現批量更新的示例代碼7. mysql 模糊查詢 concat()的用法詳解8. Oracle故障處理Rman-06207&Rman-06214的方法9. SQL Server 2005-如何在SQL Server用戶自訂函數中調用GetDate()函數10. 恢復從 Access 2000、 Access 2002 或 Access 2003 中數據庫刪除表的方法
