Mybatis判斷if中字符串等於比較遇到的坑

平時開發中咱們常常用到java

<if test="topicType != null and topicType != ''">
    AND a.topic_type = #{topicType}
</if>

這種形式的判斷,當我用「==」判斷時出現了一個奇怪的問題sql

代碼以下:mybatis

<if test="createBy != null and topicType == '1'">
    AND a.create_by = #{createBy.id}
</if>

當我這兩個條件都知足時這個查詢提件依然不能追加。開發

將其改成:string

<if test="createBy != null and topicType == '1'.toString()">
    AND a.create_by = #{createBy.id}
</if>

或者test

<if test='createBy != null and topicType == "1"'>
    AND a.create_by = #{createBy.id}
</if>

以後,個人查詢條件就能夠正常使用了。查詢

mybatis是用OGNL表達式來解析的,在OGNL的表達式中,’1’會被解析成字符,java是強類型的,char 和 一個string 會致使不等,因此if標籤中的sql不會被解析。 單個的字符要寫到雙引號裏面或者使用.toString()才行!top

相關文章
相關標籤/搜索