카테고리 없음

마이바티스 NumberFormatException 에러

한둥둥 2025. 2. 20. 10:46

Cause: java.lang.NumberFormatException: For input string: "Y"

 

mybatis 사용 시, 위에와 같은 문구 에러가 발생하였다. 

 

<if test="registSupplementFlag != null and registSupplementFlag.equals('Y')">
                AND registSupplement = 'R'
</if>

 

기존 구문은 이런식으로 있었음. 

쿼리 확인하고 debug찍고 전부해보아도 'Y'가 문자열이였다. 왜 이런 문제가 발생할까? 

 

원인 

조건문에서 홀따옴표 (') 안에 한 글자만 있을 경우 String 아닌 char로 인식하고, 이것을 숫자로 변환하려다가 보니 

(문자==숫자)의 비교처리가 되어 NumberFormatException 발생함 

 

기본적으로 char 16진수 4자리 숫자로 문자를 표기할 수 있음으로, 0~65535까지의 숫자를 대입 가능 

마이바티스 내부적으로 Y를 숫자 89로 인식하지 않았나 싶음. 

 

문자 10진수 16진수
Y 89 0x59
N 78 0x4E

 

 

해결 방안 

 

문자가 한글자인 경우, 아래와 같은 방법으로 비교가능하다. 

1. equals() 함수를 사용

<if test='registSupplementFlag != null and registSupplementFlag.equals("Y")'>
    AND registSupplement = 'R'
</if>

 

2. 홀따옴표('')를 HTML 방식으로 변경해서 사용

<if test="registSupplementFlag != null and registSupplementFlag == &quotY&quot; ">
                AND registSupplement = 'R'
</if>

 

3. toString() 함수를 사용하여 문자를 String으로 변환 

<if test="registSupplementFlag != null and registSupplementFlag == 'Y'.toString() ">
                AND registSupplement = 'R'
</if>

 

4. 홑따옴표('')아닌 쌍따옴표("")를 사용

<if test='registSupplementFlag != null and registSupplementFlag == "Y" '>
                AND registSupplement = 'R'
</if>