mybatis判断字符串是否相等采坑记


今日思语:受过生活的虐,很容易爱上 四下无人的街,对酒当歌的夜~

在处理判断条件时,经常会以某个值进行判断走不同的逻辑,mybatis中判断字符串相等时,如下:

<if test="loginIpAlias == '1'">
    <choose>
        <when test="loginIpOld != null and loginIpOld != ''">
            <if test="loginIp != null and loginIp != ''">
                and login_ip = #{loginIp}
            if>
        when>
        <otherwise>
            and login_ip = #{loginIpOld}
        otherwise>
    choose>
if>

但在运行过程中并没有生效,解决办法:

1、将外层双引号改为单引号,如下:

<if test='loginIpAlias == "1"'>

2、使用toString()方法,如下:

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

以上处理方式就可以解决字符串判断失效的问题