생각해보면 지금까지 진행한 내용중에 약간 이상한 부분이 있다.
ItemMapper 매퍼 인터페이스의 구현체가 없는데 어떻게 동작한 것일까?
ItemMapper 인터페이스
package hello.itemservice.repository.mybatis;
import hello.itemservice.domain.Item;
import hello.itemservice.repository.ItemSearchCond;
import hello.itemservice.repository.ItemUpdateDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Optional;
@Mapper
public interface ItemMapper {
void save(Item item);
void update(@Param("id") Long id, @Param("updateParam") ItemUpdateDto updateDto);
Optional<Item> findById(Long id);
List<Item> findAll(ItemSearchCond itemSearch);
}
부분은 MyBatis 스프링 연동 모듈에서 자동으로 처리해주는데 다음과 같다.

1. 애플리케이션 로딩 시점에 MyBatis 스프링 연동 모듈은 @Mapper 가 붙어있는 인터페이스를 조사한다.
2. 해당 인터페이스가 발견되면 동적 프록시 기술을 사용해서 ItemMapper 인터페이스의 구현체를 만든다.
3. 생성된 구현체를 스프링 빈으로 등록한다.
MyBatisItemRepository - 로그 추가
@Override
public Item save(Item item) {
log.info("itemMapper class={}", itemMapper.getClass());
itemMapper.save(item);
return item;
}
실행해서 주입 받은 ItemMapper 의 클래스를 출력해보자.
실행 결과
itemMapper class=class com.sun.proxy.$Proxy66
출력해보면 JDK 동적 프록시가 적용된 것을 확인할 수 있다.
매퍼 구현체
정리