Mybatis 字符串比较的坑

问题

使用下例方法比较字符串

<if test="status != null and status !='0'">
	and STATUS=0
</if>

会出现两个问题

  • 当status值为“0”,test里面的条件还是会满足
  • 当status值为“1,2,3”时,会报java.lang.NumberFormatException错误

原因:mybatis是使用OGNL表达式进行解析的,所以单个字符会被解析成char类型,java中char和String不相等

解决方法

将代码修改成

<if test='status != null and status !="0"'>
	and STATUS=0
</if>
posted @ 2022-10-24 20:56  享受生活2023  阅读(77)  评论(0编辑  收藏  举报