#ResultMap
스프링 프레임워크에서 XML파일에서 SQL을 처리할때(Mybatis)
테이블의 컬럼명과 자바Bean(VO, DTO)의 필드명이 다를때 매칭 시켜주는 기능
SQL문 최상위에 작성해야 한다
이러한 문제가 발생하는 원인은 DB에 따라 대소문자 구분방식이 다르기 때문에
컬럼명을 작성할때 _언더바를 쓰는 경우가 있다(postgreSQL)
# 작성법
<resultMap id="boardMap" type="com.test.site.Entity.BoardVO">
<id column="GOODS_NUMBER" --필수, 변환 대상 이름
property="goodsNumber" --필수, 변환 할 이름
jdbcType="" --옵션, DB컬럼 데이터 타입 명시적 선언
javaType="" --옵션, Bean 필드 데이터 타입 명시적 선언
nullValue="" --옵션, 해당 컬럼 값이 null일 경우 대신 반환할 값
/>
</resultMap>
#테이블 컬럼명과 Bean의 필드명이 일치하는 경우
<select id="selectBoard" resultType="com.test.site.Entity.BoardVO">
SELECT * FROM 테이블
</select>
#테이블 컬럼명과 Bean의 필드명이 일치하지 않는 경우
resultMap의 result 속성은 SELECT 결과의 컬럼명에 매칭된다
<resultMap id="boardMap" type="com.test.site.Entity.BoardVO">
<result column="GOODS_NUMBER" property="goodsNumber"/>
<result column="GOODS_NAME" property="goodsName"/>
<result column="GOODS_ID" property="goodsId"/>
<result column="GOODS_PRICE" property="goodsPrice"/>
<result column="GOODS_COST" property="goodsCost"/>
<result column="INSERT_DATE" property="insertDate"/>
<result column="UPDATE_DATE" property="updateDate"/>
<result column="FILE_NAME" property="fileName"/>
</resultMap>
<select id="selectBoard" resultMap="boardMap">
select * from mst_goods
</select>
SELECT문의 resultType대신 resultMap을 적어주고
resultMap을 작성하면 된다
# resultMap의 id가 SELECT문의 resultMap에 작성하는 id이다
# resultMap의 type은 기존 SELECT문의 resultType을 적어주면 된다(int, String, Bean경로)
resultMap의 result 속성은 column을 property로 변환한다
아래 코드는 테이블 컬럼명 GOODS_NUMBER를 goodsNumber로 변환한다
<resultMap id="boardMap" type="com.test.site.Entity.BoardVO">
<result column="GOODS_NUMBER" property="goodsNumber"/>
그리고 변환한 property명을 type에 반환한다