Spring
스프링 - Mybatis 사용 (Mapper)
마루설아
2025. 5. 20. 21:13
Mapper
스프링에서 SQL 처리를 XML, 어노테이션의 형태로 작성할 수 있다.
1. 어노테이션 SQL 사용하기 (쿼리가 간단할 때 용이)
src/main/java 내
테스트 인터페이스 (interface) 작성
package com.myspring.mapper;
import org.apache.ibatis.annotations.Select;
public interface TimeMapper {
@Select("SELECT SYSDATE FROM DUAL")
public String getTime();
}
root-context.xml의 Namespaces에서 mybatis-spring에 체크
root-context.xml에 아래 내용 추가
(com.myspring.mapper 패키지 내에서 Mybatis Mapper로 사용할 인터페이스를 스캔 후 Beans에 등록한다는 의미)
...
<!-- Mybatis Mapper 테스트 -->
<mybatis-spring:scan base-package="com.myspring.mapper" />
...
Mapper 테스트를 위한 자바 클래스 작성
package com.myspring.persistence;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.myspring.mapper.TimeMapper;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class TimeMapperTests {
@Setter(onMethod_ = @Autowired)
private TimeMapper timeMapper;
@Test
public void testGetTime() {
log.info(timeMapper.getClass().getName());
log.info(timeMapper.getTime());
}
}
아래 로그가 나온다면 정상
(붉은 경고 로그는 자바 상위버전에서 내부 API 접근을 제한하는 관련 내용)
2. XML SQL 사용하기 (쿼리가 복잡할 때 용이)
인터페이스에 getTime2(); 함수를 추가로 선언한다.
package com.myspring.mapper;
import org.apache.ibatis.annotations.Select;
public interface TimeMapper {
@Select("SELECT SYSDATE FROM DUAL")
public String getTime();
public String getTime2();
}
src/main/resources에 com/myspring/mapper/TimeMapper.xml을 새로 생성하여 작성한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybaatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.myspring.mapper.TimeMapper" >
<select id="getTime2" resultType="string">
SELECT SYSDATE FROM DUAL
</select>
</mapper>
테스트 클래스 파일에 testGetTime2() 메소드를 추가한다. (최종 클래스 파일)
디버깅 구분을 쉽게 하고자 로그 내용도 약간 변경하였다.
package com.myspring.persistence;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.myspring.mapper.TimeMapper;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class TimeMapperTests {
@Setter(onMethod_ = @Autowired)
private TimeMapper timeMapper;
@Test
public void testGetTime() {
log.info(timeMapper.getClass().getName());
log.info("TEST CASE 1 : " + timeMapper.getTime());
}
@Test
public void testGetTime2() {
log.info("getTime2");
log.info("TEST CASE 2 : " + timeMapper.getTime2());
}
}
아래 내용처럼 콘솔 로그가 출력된다면 정상
(* JUnit 테스트는 순서를 보장하지 않는다. 순서 지정이 필요하다면 @TestMethodOrder 어노테이션이 필요하다.)