mybatis中多条件判断---choose when的用法

    <select id="getFunctionByPage" resultMap="FunctionRlt">
        SELECT
        K.FUNCTION_NAME,K.FUNCTION_NO,K.URL,K.PARAM_CLASS,
        FROM PUB_FUNCTION K
        <choose>
            <when test="model.parentFuncName!= null and model.parentFuncName!= ''">
                ,PUB_FUNCTION PF WHERE K.PARENT_FUNCTION_ID=PF.FUNCTION_ID
                AND PF.FUNCTION_NAME LIKE '%'||#{model.parentFuncName}||'%'
                <if test="model.functionName != null and model.functionName != ''">
                    AND K.FUNCTION_NAME LIKE '%'||#{model.functionName}||'%'
                </if>
                <if test="model.functionType != null and model.functionType != ''">
                    AND K.FUNCTION_TYPE = #{model.functionType}
                </if>
                <if test="model.isMobile != null and model.isMobile != ''">
                    AND K.IS_MOBILE = #{model.isMobile}
                </if>
            </when>
            <!-- 根据父功能点获取子功能列表 -->
            <when test="model.functionId != null and model.functionId != ''">
                <where>
                    <choose>
                        <when test="model.functionId==000000L">
                            AND K.PARENT_FUNCTION_ID is null
                        </when>
                        <otherwise>
                            AND K.PARENT_FUNCTION_ID=#{model.functionId}
                        </otherwise>
                    </choose>
                </where>
            </when>
            <otherwise>
                <where>
                    <if test="model.functionName != null and model.functionName != ''">
                        AND K.FUNCTION_NAME LIKE
                        '%'||#{model.functionName}||'%'
                    </if>
                    <if test="model.functionType != null and model.functionType != ''">
                        AND K.FUNCTION_TYPE = #{model.functionType}
                    </if>
                    <if test="model.isMobile != null and model.isMobile != ''">
                        AND K.IS_MOBILE = #{model.isMobile}
                    </if>
                </where>
            </otherwise>
        </choose>
        <choose>
            <when test="sort != null and sort != '' and dir != null and dir != ''">
                ORDER BY ${sort} ${dir}
            </when>
            <otherwise>
                ORDER BY K.PARENT_FUNCTION_ID,K.FUNCTION_SEQ ASC
            </otherwise>
        </choose>
    </select>

 

posted @ 2017-02-13 10:06  StanLong  阅读(9584)  评论(0编辑  收藏  举报