Mybatis if test 字符串比较不生效

叁歲伎倆 2022-05-18 05:08 365阅读 0赞
  1. <if test="publishType!='2'">
  2. and t.status='3'
  3. and t.has_attachment='YES'
  4. </if>

其中publishType为传来的String类型参数,想比较其不等于字符串2,但是判断不生效

原因:

单引号是char类型,双引号是string类型!

char表示字符,定义时使用用单引号表示,只能存储一个字符。

而String表示字符串,定义时使用双引号表示,可以存储0个或多个字符,其实string类型就是char类型的数组表现形式。

所以’2’被认为是char类型,和String类型不相等

解决方法:

1.改成双引号

  1. <if test='publishType!="2"'>
  2. and t.status='3'
  3. and t.has_attachment='YES'
  4. </if>

2.加.toString()方法

  1. <if test="publishType!='2'.toString()">
  2. and t.status='3'
  3. and t.has_attachment='YES'
  4. </if>

发表评论

表情:
评论列表 (有 0 条评论,365人围观)

还没有评论,来说两句吧...

相关阅读