Mybatis的mapper.xml中if标签test判断的用法说明

mapper.xml中if标签test判断的用法

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

① 将双引号和单引号的位置互换

1

2

3

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

   AND 表字段 = #{testString}

</if>

② 加上.toString()

1

2

3

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

  AND 表字段 = #{testString}

</if>

2. 非空条件的判断

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

1

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

但是这样的判断只是针对String的,如果是别的类型,这个条件就不一定成立了,比如最经典的:当是数字0时,这个判断就会把0过滤掉,所以如果要判断数字,我们一般会再加上一个0的判断(这和mybatis的源码逻辑有关,有兴趣的可以去看看源码)

1

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

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

答案是可以的。

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

1

<if test="@完整的包名类名@方法名(传参)">

例如:

1

<if test="@com.xxx.util.MybatisTestUtil@isNotEmpty(obj)">

下面是我写的一个简陋的工具类,不是很全面,抛砖引玉,各位可以根据需要补充。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

import java.util.Collection;

import java.util.Map;

/**

 * @description: mybatis的<if test="">标签中使用的非空判断工具类

 *      使用方式:<if test="@com.xxx.xxx.util.MybatisTsetUtil@isNotEmpty(obj)">

 * @author: singleDog

 * @date: 2020/7/20

 */

public class MybatisTestUtil {

    public static boolean isEmpty(Object o) {

        if (o == null) {

            return true;

        }

        if (o instanceof String) {

            return ((String) o).trim().length() == 0;

        } else if (o instanceof Collection) {

            return ((Collection) o).isEmpty();

        } else if (o instanceof Map) {

            return ((Map) o).isEmpty();

        } else if (o.getClass().isArray()) {

            return ((Object[]) o).length == 0;

        } else {

            return false;

        }

    }

    public static boolean isNotEmpty(Object o) {

        return !isEmpty(o);

    }

}

3. 判断数组是否包含某个元素

1

2

3

<if test="list.contains(xxx)">

    //...

</if>

注意,元素类型是字符串的话,参考1中的写法,一般这样写

1

2

3

4

5

6

7

8

9

<!--包含-->

<if test="list.contains('示例元素'.toString())">

    //...

</if>

<!--不包含-->

<if test="!list.contains('示例元素'.toString())">

    //...

</if>

mapper.xml <if test>书写时候的一些坑

1. 分页

map中添加了两个int类型的数据,

1

2

map.put("startNum",(page.getPageNum()-1)*page.getNumPerPage());

map.put("pageSize",page.getNumPerPage());

错误的SQL书写:

1

2

3

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

       LIMIT #{startNum},#{pageSize}

</if>

正确的SQL书写

1

2

3

<if test="startNum != null">

       LIMIT #{startNum},#{pageSize}

</if>

因为startNum里存的是int数据,所以不能与空字符串进行比较,强行比较时会报错。

2. 字符串形式的数据比较

map中添加的是一个字符串形式的“1”

1

map.put("uploadFlag",upload.getUploadFlag());

如果想在XML中比较,以下两种方式都可以:

2.1 test使用双引号

比较的对象使用单引号点toString()方法:

1

2

3

<if test="uploadFlag=='1'.toString()">

     and pw.id in (select pw_id from t_contract_upload)

</if>

2.2 test使用单引号

比较的对象直接使用双引号:

1

2

3

<if test='uploadFlag=="2" '>

     and pw.id in (select pw_id from t_contract_upload)

</if>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值