Spring, Spring Boot

[Spring Boot] MyBatis typeAlias 지정하기

Yoon 2022. 4. 1. 13:10

MyBatis를 이용해 쿼리를 작성할 때 리턴 타입이나 파라미터 타입 등을 지정할 때가 있는데,

위 사진처럼 패키지 명을 모두 작성해야 하는 번거로움이 있다.

 

이를 개선하기 위해 MyBatis는 typeAlias를 지정할 수 있는 설정기능을 제공하고있다.

mybatis-config.xml

위 사진과 같이 typeAilas를 지정하면 해당 패키지의 객체를 지정한 별칭으로 파라미터를 지정할 수 있다.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0/EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <typeAlias alias="별칭" type="전체패키지명"/>
    </typeAliases>
</configuration>

mybatis-config.xml파일을 만들어 위 코드를 넣으면 되는데,

 

application.properties에도 mybatis-config.xml을 설정파일로 쓰겠다고 명시해줘야한다.

(설정 xml파일 이름을 꼭 mybatis-config로 안해도된다.)

mybatis.config-location = classpath:mybatis-config.xml