Mybatis xml if标签判断

1. 字符串等于条件有两种写法

//1. 将双引号和单引号的位置互换
<if test=' testString != null and testString == "A" '>
   AND 表字段 = #{testString}
</if>

//2. 加上.toString()
<if test=" testString != null and testString == 'A'.toString() ">
  AND 表字段 = #{testString}
</if>


2. 非空条件的判断
长久以来,我们判断非空非null的判断条件都是如下所示:

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

但是如果传进来的是数组或者集合呢?我们要再写别的判断吗?能不能封装个方法呢?

答案是可以的。

if标签里面的test判断是可以使用工具类来做判断的,毕竟test后面跟的也是一个布尔值,其用法是:

<if test="@完整的包名类名@方法名(传参)">
例如:
<if test="@com.xxx.util.MybatisTestUtil@isNotEmpty(obj)">

猜你喜欢

转载自blog.csdn.net/qq_38623939/article/details/128144696
今日推荐