Mybatis if

    원본 http://xshine.tistory.com/249


    <select id="get" parameterType="map" resultMap="resultMap">

        SELECT         *
        FROM         테이블명
        <where>        
            <if test="파라미터 != null and 파라미터 == 'A'">
                    AND 필드명 = #{파라미터}
            </if>
        </where>                
    </select>


    위와 같은 쿼리 실행시 NumberFormatException이 발생 한다면
    아래와 같이 코드를 수정 하면 해결 될 것이다.

    <select id="get" parameterType="map" resultMap="resultMap">
        SELECT         *
        FROM         테이블명
        <where>        
            <if test="파라미터 != null and (파라미터 eq 'A'.toString())">
                    AND 필드명 = #{파라미터}
            </if>
        </where>                
    </select>   


    OR

    <select id="get" parameterType="map" resultMap="resultMap">
        SELECT         *
        FROM         테이블명
        <where>        
            <if test="파라미터 != null and 파라미터.equals('A')">
                    AND 필드명 = #{파라미터}
            </if>
        </where>                
    </select>   


    보너스 (대소문자 무시)
    <select id="get" parameterType="map" resultMap="resultMap">
        SELECT         *
        FROM         테이블명
        <where>        
            <if test="파라미터 != null and 파라미터.equalsIgnoreCase('A')">
                    AND 필드명 = #{파라미터}
            </if>
        </where>                
    </select>   


    댓글