mybatis中使用if标签比较两个字符串是否相等

 <!-- 此处使用if比较是否相等 -->

范例一:

<select id="findClientIds" parameterType="map" resultType="map">
        SELECT sys_user.id,sys_user.clientId FROM sys_user
        <where>
            <if test="grade!= null and grade!= ''and grade == '3'.toString()">
                id =(
                SELECT PRIMARY_PERSON FROM sys_office WHERE id = 
                (SELECT office_id FROM sys_user WHERE id = #{userId}
                ))
            </if>
            <if test="grade!= null and grade!= '' and grade == '2'.toString()">
                id =(
                SELECT PRIMARY_PERSON FROM sys_office WHERE id = 
                (SELECT company_id FROM sys_user WHERE id = #{userId}
                ))
            </if>
            <if test="grade!= null and grade!= '' and grade == '1'.toString()">
                id = ''
            </if>
        </where>
    </select>

范例二:

mybatis 映射文件中,if标签判断字符串相等,两种方式:
因为mybatis映射文件,是使用的ognl表达式,所以在判断字符串sex变量是否是字符串Y的时候

<if test="sex=='Y'.toString()">

或者使用下面的写法 <if test = 'sex== "Y"'>

注意:
不能使用

<if test="sex=='Y'">
and 1=1
</if>
 

因为mybatis会把'Y'解析为字符,java是强类型语言,所以不能这样写。

你的能量超乎你想象_________

 

原文地址:https://www.cnblogs.com/a8457013/p/8033549.html