mybatis中的if-else语句!!!

时间:2025-04-25 07:11:34

语法:
 

            <choose>
                <when test="recruitType != null and recruitType != ''">
                    test中的条件成立
                </when>
                <otherwise>
                    不成立
                </otherwise>
            </choose>


            或者
        <if test='auditstate=="2"'>
            and  is not null
        </if>

例子:
mapper:
        

List<Map<String, Object>> listHotPositions( @Param("recruitType") String recruitType);
====================================================================================
<select  resultType="Map" parameterType="String">
    select
        POSITIONID positionId,
        POSITIONNAME positionName
    from
        rec_position
    where
        STATE = 02 
        <choose>
            <when test=" recruitType != null and recruitType != '' ">
                and RECRUITTYPE = #{recruitType}
            </when>
            <otherwise>
                and RECRUITTYPE in ('01','02')
            </otherwise>
        </choose>
</select>

或者:

<choose>
    <when test="itemCode == 'ITEM007'.toString()">
        CASE WHEN #{itemCode} = 'ITEM007' and kd.${itemCode} > 0 then
        concat('迟到',kd.ITEM006, '分钟') ELSE '' END
    </when>
    <when test="itemCode == 'ITEM009'.toString()">
        CASE WHEN #{itemCode} = 'ITEM009' and kd.${itemCode} > 0 then
        concat('早退',kd.ITEM007, '分钟') ELSE '' END
    </when>
    <when test="itemCode == 'ITEM012'.toString()">
        CASE WHEN #{itemCode} = 'ITEM012' and (kd.${itemCode} > 0 or ksbd.SIGN_IN_TIME is null or SIGN_OUT_TIME is null) and ksbd.IS_IN_SHIFT = 1 and ksbd.SIGN_STATUS > 1 then
        '旷工' ELSE '' END
    </when>
    <when test="itemCode not in {'ITEM007'.toString(),'ITEM009'.toString().toString(),'ITEM012'.toString()}">
        CASE WHEN #{itemCode} not in ('ITEM007','ITEM009','ITEM012') and kd.${itemCode} > 0 then
        #{ItemAlias} ELSE '' END
    </when>
    <otherwise>
        ''
    </otherwise>
</choose>

========================================================================