본문 바로가기

DATABASE/Mybatis

[Mybatis] ResultType(ResultClass) : ResultMap 차이점

 

 

https://mybatis.org/mybatis-3/sqlmap-xml.html

 

mybatis – MyBatis 3 | Mapper XML Files

Mapper XML Files The true power of MyBatis is in the Mapped Statements. This is where the magic happens. For all of their power, the Mapper XML files are relatively simple. Certainly if you were to compare them to the equivalent JDBC code, you would immedi

mybatis.org

 

 

Mybatis 공식 문서에 기재된 차이점.

 

resultType The fully qualified class name or alias for the expected type that will be returned from this statement. Note that in the case of collections, this should be the type that the collection contains, not the type of the collection itself. Use resultType OR resultMap, not both.
resultMap A named reference to an external resultMap. Result maps are the most powerful feature of MyBatis, and with a good understanding of them, many difficult mapping cases can be solved. Use resultMap OR resultType, not both.

 

 

ResultType

  • Ibatis : ResultClass -> Mybatis : ResultType
  • 전체 클래스명 또는 Alias를 입력한다. (Mapping 하려는 Java Class의 전체 경로를 입력)
ex) com.test.Example 객체로 쿼리 실행 결과 값을 받고자 할 때
	<select id="selectTest" resultType="com.test.Example">
    		...
    	</select>
    
ex) String형 객체로 쿼리 실행 결과 값을 받고자 할 때
	<select id="selectTest" resultType="String">
    		...
    	</select>

 

 

ResultMap

  • 선언 당시 참조로 사용한 이름을 입력한다.
  • 개발자가 직접 원하는 POJO Class에 Mapping 가능하다. ( ResultType은 자동 Mapping 되지만 제한이 있음
ex) 
	<resultMap id="test" type="com.test.Example">
    		<result property="name" column="name">
        	...
    	</resultMap>
    
	<select id="selectTest" resultType="test">
    		...
    	</select>