Programming/Spring
[MyBatis] if 에서 문자열 비교시 NumberFormatException 해결 방법
록씨
2018. 2. 1. 14:36
반응형
<select id="get" parameterType="map" resultMap="resultMap">
SELECT *
FROM 테이블명
<where>
<if test="파라미터 != null and 파라미터 == 'A'">
AND 필드명 = #{파라미터}
</if>
</where>
</select>
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>
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 *
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>
SELECT *
FROM 테이블명
<where>
<if test="파라미터 != null and 파라미터.equalsIgnoreCase('A')">
AND 필드명 = #{파라미터}
</if>
</where>
</select>
출처: http://xshine.tistory.com/249 [메모하는습관]
반응형