Mybatis的if test字符串比较问题

怼烎@ 2023-10-17 15:22 252阅读 0赞

第一种解决方法:

  1. <if test="isExpired=='Y'">
  2. and msg.expire_time < now()
  3. </if>
  4. 会报NumberFormatException,这样就可以了。
  5. <if test="isExpired=='Y'.toString()">
  6. and msg.expire_time < now()
  7. </if>
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. 6
  14. 7
  15. 1
  16. 2
  17. 3
  18. 4
  19. 5
  20. 6
  21. 7

第二种解决方法

  1. <if test=" name=='你好' ">
  2. <if>
  3. 这样会有问题,换成
  4. <if test=' name=="你好" '>
  5. <if>

实际用到的地方是这样的
当一个条件既要用到等于又要用到>等判断的时候这样做

  1. <if test="_parameter.containsKey('colNum') and colNum!='6'.toString()" >
  2. and a.col_num = #{colNum}
  3. </if>
  4. <if test="_parameter.containsKey('colNum') and colNum =='6'.toString()" >
  5. and (a.col_num+0) > 5
  6. </if>
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6

非常方便

如果用<号 会冲突和<if,是这样解决的

  1. <![CDATA[<if test="_parameter.containsKey('sendAreaName')" >
  2. and a.send_area_name <#{sendAreaName}
  3. </if>]]>
  4. 1
  5. 2
  6. 3
  7. 1
  8. 2
  9. 3

把if包在里面就好了

发表评论

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

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

相关阅读