반응형

오늘은 MyBatis에서 제공하는 ResultMap을 활용해 복잡한 객체 매핑을 처리하는 방법에 대해 알아보겠습니다. 데이터베이스의 테이블 구조와 자바 객체 간의 매핑을 유연하게 정의할 수 있어 실무에서 매우 유용합니다.


1. ResultMap의 필요성

기본적으로 MyBatis는 SQL 쿼리 결과를 자바 객체와 자동으로 매핑해줍니다. 하지만, 다음과 같은 상황에서는 기본 매핑으로는 부족할 수 있습니다.

  • 테이블의 컬럼 이름이 객체의 필드 이름과 다를 때.
  • 조인된 결과를 하나의 객체로 매핑해야 할 때.
  • 특정 컬럼 값을 다른 필드에 매핑하거나 가공해야 할 때.

이럴 때 ResultMap을 사용하여 매핑 규칙을 명시적으로 정의할 수 있습니다.


2. ResultMap 기본 사용법

(1) 예제 테이블

employees와 departments 테이블을 사용하여 조인 결과를 매핑해보겠습니다.

employees 테이블

Column Name Type Description

employee_id INT 사원 ID
first_name VARCHAR 이름
last_name VARCHAR
department_id INT 부서 ID

departments 테이블

Column Name Type Description

department_id INT 부서 ID
department_name VARCHAR 부서명

(2) 매핑할 자바 객체

Employee 클래스와 Department 클래스를 정의합니다.

package com.example.model;

public class Department {
    private int departmentId;
    private String departmentName;

    // Getters and Setters
}

public class Employee {
    private int employeeId;
    private String firstName;
    private String lastName;
    private Department department;

    // Getters and Setters
}

3. ResultMap 정의

EmployeeMapper.xml 파일에서 ResultMap을 정의합니다.

<mapper namespace="com.example.mapper.EmployeeMapper">

    <resultMap id="EmployeeResultMap" type="com.example.model.Employee">
        <id property="employeeId" column="employee_id"/>
        <result property="firstName" column="first_name"/>
        <result property="lastName" column="last_name"/>
        <association property="department" javaType="com.example.model.Department">
            <id property="departmentId" column="department_id"/>
            <result property="departmentName" column="department_name"/>
        </association>
    </resultMap>

    <select id="findAllEmployees" resultMap="EmployeeResultMap">
        SELECT e.employee_id, e.first_name, e.last_name, d.department_id, d.department_name
        FROM employees e
        LEFT JOIN departments d ON e.department_id = d.department_id
    </select>

</mapper>

4. Mapper 인터페이스 작성

package com.example.mapper;

import com.example.model.Employee;
import java.util.List;

public interface EmployeeMapper {
    List<Employee> findAllEmployees();
}

5. 테스트 코드 작성

findAllEmployees 메서드를 호출해 모든 직원 정보를 조회합니다.

package com.example;

import com.example.mapper.EmployeeMapper;
import com.example.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TestRunner implements CommandLineRunner {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void run(String... args) throws Exception {
        List<Employee> employees = employeeMapper.findAllEmployees();

        if (!employees.isEmpty()) {
            System.out.println("직원 목록:");
            employees.forEach(employee -> {
                System.out.println("ID: " + employee.getEmployeeId());
                System.out.println("이름: " + employee.getFirstName() + " " + employee.getLastName());
                if (employee.getDepartment() != null) {
                    System.out.println("부서: " + employee.getDepartment().getDepartmentName());
                } else {
                    System.out.println("부서: 없음");
                }
                System.out.println("----------------------------");
            });
        } else {
            System.out.println("직원 정보가 없습니다.");
        }
    }
}

6. ResultMap의 주요 태그

  1. <id>: 주 키를 매핑합니다.
  2. <result>: 일반적인 컬럼을 매핑합니다.
  3. <association>: 객체와 객체 간의 관계(1:1 관계)를 매핑합니다.
  4. <collection>: 객체와 컬렉션 간의 관계(1:N 관계)를 매핑합니다.

7. 오늘의 요약

  • ResultMap은 MyBatis에서 SQL 결과를 자바 객체에 유연하게 매핑할 수 있도록 도와줍니다.
  • 객체 간의 관계를 매핑할 때는 <association>과 <collection> 태그를 사용합니다.
  • SQL 조인 결과를 효율적으로 매핑하려면 ResultMap을 활용하세요.
반응형

+ Recent posts