平时开发中我们经常用到
<if test="topicType != null and topicType != ''"> AND a.topic_type = #{topicType} </if>
这种形式的判断,当我用“==”判断时出现了一个奇怪的问题
代码如下:
<if test="createBy != null and topicType == '1'"> AND a.create_by = #{createBy.id} </if>
当我这两个条件都满足时这个查询提件依然不能追加。
将其改为:
<if test="createBy != null and topicType == '1'.toString()"> AND a.create_by = #{createBy.id} </if>
或者
<if test='createBy != null and topicType == "1"'> AND a.create_by = #{createBy.id} </if>
之后,我的查询条件就可以正常使用了。
mybatis是用OGNL表达式来解析的,在OGNL的表达式中,’1’会被解析成字符,java是强类型的,char 和 一个string 会导致不等,所以if标签中的sql不会被解析。 单个的字符要写到双引号里面或者使用.toString()才行!