Q. resultMap 언제 사용하는가?
A. 컬럼명과 프로퍼티명이 다른 경우, 명시적인 resultMap 을 선언하는 방법이 있다.
JAVA 소스상에서 사용시 스네이크 기법을 많이 사용하기 때문에, 컬럼명과 프로퍼티명이 일치하지 않을 때가 있다.
resultMap은 열 이름 불일치를 해결하는 방법 중 하나이다.
컬럼명이란?
쿼리구문에서 사용하는 DB 컬럼명 (ex. seq_no)
프로퍼티명이란?
package com.java.someapp.User;
public class UserVO {
private int id; // 얘가 프로퍼티
private String username; // 얘가 프로퍼티
private String hashedPassword; // 얘가 프로퍼티
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getHashedPassword() {
return hashedPassword;
}
public void setHashedPassword(String hashedPassword) {
this.hashedPassword = hashedPassword;
}
}
자바빈 스펙에 기반하여, 위 클래스는 3개의 프로퍼티(id, username, hashedPassword)를 가진다.
정리하자면 mybatis의 쿼리에서 사용하는 컬럼명과, 내가 VO로 사용하겠다 선언한 프로퍼티명이 다를경우 mybatis의 resultMap을 사용해서 매핑을 시켜주는 것이다.
myBatis - resultMap 예시
<resultMap id="selectUserInfo" type="com.java.someapp.UserVO">
<result property ="id" column ="USER_ID"/>
<result property ="username" column ="USER_NAME" />
</resultMap>
<select id="getMemberInfo" parameterType="com.java.someapp.vo.UserVO" resultMap="selectUserInfo">
SELECT USER_ID, USER_NAME
FROM MEMBER
</select>
<resultMap> 태그의 id명을 보면 selectUserInfo로 선언했다. 이를 사용하고자 하는 <select> 구문에서의 resultMap과 일치 시키면 된다.
resultMap을 사용하지 않는 경우?
하지만 resultMap을 항상 사용하는 것은 아닌데 그 경우는 아래와 같다.
1. 컬럼명과 프로퍼티명이 일치할 때
컬럼명과 프로퍼티명이 일치하기 때문에 resultMap을 사용하지 않아도 된다.
<select id="selectUsers" resultType="com.someapp.model.User">
select id, username
from some_table
where id = #{id}
</select>
2. Alias를 지정해서 사용하기
Alias를 지정하여 프로퍼티명과 일치 시켜서 사용하면 resultMap을 사용하지 않아도 된다.
myBatis에서 자동으로 resultMap을 만들어 준다고 한다.
"이러한 경우 MyBatis는 이름을 기준으로 열을 JavaBean 속성에 자동 매핑하기 위해 자동으로 뒤에 ResultMap을 만듭니다. 열 이름이 정확하게 일치하지 않으면 열 이름에 select 절 별칭(표준 SQL 기능)을 사용하여 레이블을 일치시킬 수 있습니다."
예시는 다음과 같다.
<!-- In Config XML file -->
<!-- typeAlias를 지정하면 resultType에서 alias만 적어도 사용가능 하다. 매번 풀 경로 적지 않아도됨.-->
<!-- mybatis 설정 파일에서 myBatis 기본경로를 지정하는 방법도 있음. -->
<typeAlias type="com.someapp.model.User" alias="User"/>
<select id="selectUsers" resultType="User">
SELECT USER_ID as "id"
, USER_NAME as "userName"
FROM MEMBER
</select>
resultMap 의 속성
resultMap의 id 에는 사용할 임의의 이름을 기술하고, type 에는 패키지를 포함한 자바 클래스명이나 타입별칭을 적어준다.
속성 | 설명 |
id | 결과매핑을 참조하기 위해 사용할 수 있는 값으로, 네임스페이스에서 유일한 식별자. - 예시 <resultMap id ="resultMapEx" type ="com.java.someapp.User"> . . . </resultMap> - 쿼리 구문의 resultMap에 사용하면 된다. <select id="selectListEx" resultMap ="resulltMapEx" > SELECT * FROM .. </select> |
type | 패키지를 포함한 자바 클래스명이나 타입별칭(typeAlias), 보통 클래스명을 지정해줌. - 예시 <resultMap id="resultMapEX" type ="com.java.someapp.User"> . . . </resultMap> |
mybatis.org/mybatis-3/ko/sqlmap-xml.html#Result_Maps
MyBatis – 마이바티스 3 | 매퍼 XML 파일
Mapper XML 파일 마이바티스의 가장 큰 장점은 매핑구문이다. 이건 간혹 마법을 부리는 것처럼 보일 수 있다. SQL Map XML 파일은 상대적으로 간단하다. 더군다나 동일한 기능의 JDBC 코드와 비교하면
mybatis.org
'BackEnd > MyBatis' 카테고리의 다른 글
[MyBatis] list로 넘긴 데이터를 foreach 로 where in 구문에서 사용 (0) | 2022.09.15 |
---|---|
[Spring Boot] [MyBatis] xml 에서 스네이크 표기법을 camelCase로 자동 변환 / resultMap 지정하지 않고 사용하기 (0) | 2022.06.15 |
[MyBatis] 반복되는 쿼리 묶기_<sql/><include/> (0) | 2020.08.31 |