在MyBatis的mapp文件中的if判斷中是這樣寫的
<if test="type == '0' ">
and so1.id = #{unitcode}
</if>
導致出現的問題就是根本沒有進去這個if判斷中,所以條件 and so1.id = #{unitcode} 也沒有加上。導致不執行if判斷中的sql,運行程序不報錯,沒有任何提示。去掉takeWay == “1” and 則可執行。對此我百思不得其解,
改為:
<if test= "type == '0'.toString() ">
and so1.id = #{unitcode}
</if>
或者改成:
<if test= 'type == "0" '>
and so1.id = #{unitcode}
</if>
這樣就可以使用了。
原理分析:
mybatis是用OGNL表達式來解析的,在OGNL的表達式中,’1’會被解析成字符,java是強類型的,char 和 一個string 會導致不等,所以if標簽中的sql不會被解析。
總結下使用方法:單個的字符要寫到雙引號里面或者使用.toString()才行!