代码改变世界

mybatis 中 if-test 判断详解

2023-04-25 17:12  猎手家园  阅读(4387)  评论(0)    收藏  举报

1、字符串判断,两种写法都可以

(1)将双引号和单引号的位置互换

<if test='testString != null and testString == "A"'>
   
</if>

(2)加上.toString()

<if test="testString != null and testString == 'A'.toString()">
  
</if>

 

2、非空条件判断只对字符串有效

<if test="xxx !=null and xxx !=''">

 

如果是数字,则会把0过滤掉,因此我们需要再加上一个0的判断:

<if test="xxx !=null and xxx !='' or xxx == 0">

 

实力踩坑日期类型date

invalid comparison: java.util.Date and java.lang.String

原图是Date不能进行字符串Date != ""判断,只进行Date != null判断即可!

<if test='startDate != null'>
     start_date = #{startDate, jdbcType=TIMESTAMP},
</if>