问题1 Mybatis if test 字符串比较不生效
<if test="publishType!='2'">
and t.status='3'
and t.has_attachment='YES'
</if>
其中publishType为传来的String类型参数,想比较其不等于字符串2,但是判断不生效
原因:
单引号是char类型,双引号是string类型!
char表示字符,定义时使用用单引号表示,只能存储一个字符。
而String表示字符串,定义时使用双引号表示,可以存储0个或多个字符,其实string类型就是char类型的数组表现形式。
所以'2'被认为是char类型,和String类型不相等
1.改成双引号
<if test='publishType!="2"'>
and t.status='3'
and t.has_attachment='YES'
</if>
2.加.toString()方法
<if test="publishType!='2'.toString()">
and t.status='3'
and t.has_attachment='YES'
</if>
问题2 mybatis 之 if test 条件,Integer类型 ,参数为0时,查询条件未输出。如,当 tagtype值为0(Integer),查询条件没有拼接 and tagtype=0。传入其他值(1,2,3...)都正常
一、mybatis 配置
<!-- Where查询条件 -->
<sql id="whereSQL">
<if test=" null != id">
AND id =#{id}
</if>
<if test=" null != tagname and '' != tagname">
AND tagname LIKE '%${tagname}%'
</if>
<if test=" null != tagtype and '' != tagtype">
AND tagtype = #{tagtype}
</if>
</sql>
tagtype 表中类型Integer
解决方法:
将
<if test=" null != tagtype and '' != tagtype">
AND tagtype = #{tagtype}
</if>
修改为
<if test=" null != tagtype and '' != tagtype or 0 == tagtype">
AND tagtype = #{tagtype}
</if>