네로개발일기

개발자 네로의 개발 일기, 자바를 좋아합니다 !

반응형

1. Mybatis resultMap 기능

mybatis는 ORM 기술 중 하나이다. ORM(Object Relational Mapping)이란 객체 지향 언어의 객체와 관계형 데이터를 서로 변환해준다는 것이다.

DB 조회 결과를 복잡한 객체 구조로 변환해야 할 때 mybatis의 resultMap 기능을 사용한다. 여러 테이블의 조인 결과를 여러 자바 객체에 담을 때 resultMap 기능이 유용하다.

 

2. Mybatis mapper 구현 방법

1) annotation으로 구현하기

간단한 SQL을 구현할 때 annotation을 이용하여 구현하는 것이 편하다.

2) mapper XML에서 구현하기

mybatis resultMap 기능을 구현하려면 mapper XML 파일에 구현하는 것이 편하다.

 

mapper/RegisterMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper .dtd">

<mapper namespace="mapper.RegisterMapper">
    <resultMap id="RegisterwithStudentCourse" type="dto.Register">
        <id property="id" column="id" />
        <result property="studentId" column="studentId" />
        <result property="courseId" column="courseId" />
        <result property="grade" column="grade" />
        <result property="createDate" column="createDate" />
        <association property="student" javaType="dto.Student">
            <id property="id" column="studentId" />
            <result property="studentNumber" column="studentNumber" />
            <result property="name" column="studentName" />
        </association>
        <association property="course" javaType="dto.Course">
            <id property="id" column="courseId" />
            <result property="courseName" column="courseName" />
            <result property="unit" column="unit" />
        </association>
    </resultMap>
</mapper>

 

<mapper namespace="mapper.RegisterMapper">

RegistMapper에 대한 SQL 명령이나 resultMap 등을 정의하기 위한 mapper 태그이다.

패키지까지 포함해서 mapper 클래스의 이름을 적는다.

 

<resultMap id="RegisterwithStudentCourse" type="dto.Register">

조회 결과를 Regist 객체로 채우는 방법을 정의한다. SQL 조회 결과를 Java 객체 구조에 채우는 방법을 정의한 것이 resultMap이다.

 

<id property="id" column="id" />

조회 결과의 id 칼럼은 Register 클래스의 id 속성(property)에 채운다. 이 칼럼이 테이블의 기본키(primary)이기 때문에 <id> 태그를 사용한다.

 

<result property="studentId" column="studentId" />

조회 결과의 studentId 칼럼을 Register 클래스의 studentId 속성에 채운다. 테이블의 기본키가 아니기 때문에 <result> 태그를 사용한다.

 

<association property="student" javaType="dto.Student">

Register 클래스의 student 속성에 Student 객체를 대입한다.

 

/mapper/RegisterMapper.java

import java.util.List; 

import org.apache.ibatis.annotations.Mapper; 
import org.apache.ibatis.annotations.ResultMap; 
import org.apache.ibatis.annotations.Select; 

import dto.Register; 

@Mapper 
public interface RegisterMapper { 

    @ResultMap("RegisterWithStudentAndCourse") 
    @Select("SELECT r.*, s.studentNumber, s.name studentName, c.courseName, c.unit " + 
            " FROM register r JOIN student s ON r.studentId = s.id " + 
            " JOIN course c ON r.courseId = c.id " + 
            " ORDER BY s.studentNumber ") 
    List<Register> findAll(); 
}

 

@ResultMap("RegisterWithStudentAndCourse")

 

RegisterMapper.xml 파일의 id="RegisterWithStudentAndCourse" resultMap 방법으로 조회 결과를 Java 객체들에 채워서 리턴한다.

728x90
반응형
blog image

Written by ner.o

개발자 네로의 개발 일기, 자바를 좋아합니다 !