首页 > 技术文章 > <if test="type == '0' ">没有进去这个判断的问题

timao 2019-08-27 19:34 原文

在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()才行!
 

推荐阅读