MyBatis DynamicSQL 中的 IF 判断

在写动态sql时发现字符串的判断没有生效

<if test="CLLX != null and CLLX != ''">
    and a.CLLX = #{CLLX}
</if>

当CLLX为空字符串时判断依旧会成立

 

网上查询后有以下几种解决办法

1.加上toString()

<if test="CLLX != null and CLLX != ''.toString()">
    and a.CLLX = #{CLLX}
</if>

2.改用双引号

<if test='CLLX != null and CLLX != ""'>
    and a.CLLX = #{CLLX}
</if>

3.使用自定义方法

package com;

public class Utils {
    public static Boolean isString(String str) {
        return str != null && !str.isEmpty();
    }
}
<if test="@com.Utils@isString(CLLX)">
    and a.CLLX = #{CLLX}
</if>

 

Mybatis会将 “” 解析为字符(java 强类型语言, ‘’ char 类型 ),而非字符串,不能做到判断的效果。


https://blog.csdn.net/haha_sir/article/details/79291968

https://blog.csdn.net/huahuahua0809/article/details/76637798

posted @ 2019-04-01 16:32  江湖小小白  阅读(832)  评论(0编辑  收藏  举报