본문 바로가기

Backend/DB 실무

[Mybaytis] Mybatis에러

반응형
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'cancerType' in 'class java.lang.String'

파라미터 타입이 String일때 cancerType으로 지정된 요소를 못가져 온다는 에러이다.

일반적인 경우에는 ParameterType을 Map이나 VO로 지정한다.

하지만 String으로 단일 문자열만 전달되는 경우에는 ${전달요소명} 또는 #{전달요소명}으로 작성하면 위의 에러가 발생할 수 있다고 한다.

이런 경우에는 #{value}로 대체하여 사용하면 해결된다고 한다. 하지만 Map으로 파라미터를 전달하는 방식을 권장한다고 한다.

<select id="select01List" parameterType="String" resultType="testVO">
SELECT * FROM public.sampleTable
-- 에러 발생
--<if test="data != null and data !=''">
--WHERE col = #{data}
--</if>
-- 해결 방법
<if test="value != null and value !=''">
WHERE col = #{value}
</if>

 

하지만 동일한 에러가 발생해 Map으로 변경해 보았으나 해결되지는 않고 에러가 변경되었다.

[http-nio-8750-exec-6] ERROR j.sqltiming - 1. PreparedStatement.setString(3, "") org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
...(중략)
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='cancerType', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.

 

(JAVA) String -> (parameterType) String

(JAVA) HashMap -> (parameterType) HashMap

 

으로 조건을 바꿔가면서 시도했다. 

하지만 각각 에러는 달랐지만 결론은 Parameter를 잘못 인식한다는 에러가 계속 떴다.

그러다 HashMap으로 Parameter를 전달하고 ParameterType을 String으로 주었더니 정상적으로 출력되는 것을 확인할 수 있었다.

(JAVA) HashMap -> (parameterType) String

 

반응형

'Backend > DB 실무' 카테고리의 다른 글

쿼리 단순화 작업  (0) 2024.02.19
02/16  (0) 2024.02.16
01/09 회고  (0) 2024.01.09
[Mybatis] 대소문자 구분 없이 조회  (0) 2023.11.10
[Mybatis] The column index is out of range & COALESCE(A, B)  (0) 2023.11.09