spring boot 에서 mybatis 관련 config 설정은 resources 폴더 아래 mybatis-config.xml 에 설정을 한다.
나의 경우는 oracle 의 select 구문수행 후 얻어오는 column 값을 java 의 vo 객체에 담을때 camel 형식으로
java 객체의 member 변수에 담기는 설정을 했다.
하지만 어느 순간 camel 설정이 먹지를 않았다.
예를 들어 select total_price, qty from category 수행 후 예전에는
qty 와 totalPrice 에 모두 mapping 이 되어서 값이 담겼지만 어느순간 부터는 qty 에만 값이 담겼다.
@data
public classPrice {
Integer totalPrice;
Integer qtyl
}
이유는 아래와 같다.
기본적으로 Spring Boot는 MyBatis와의 통합에서 application.yml에 정의된 설정을 우선적으로 적용한다.
나의 경우 mybatis 의 일부 설정은 mybatis-config.xml 에 있고
또 일분 설정은 application.yml 에 존재 했다.
동작 방식
- mybatis-config.xml의 역할
- MyBatis의 전통적인 설정 파일로, MyBatis 자체의 고유 설정(예: 캐싱, 매퍼 위치, 로그 설정 등)을 정의합니다.
- mybatis-config.xml의 설정은 Spring Boot와 독립적으로 작동하며, Spring Boot는 이를 명시적으로 로드하지 않는 한 자동으로 적용되지 않습니다.
- application.yml의 역할
- Spring Boot와 통합된 MyBatis 설정을 정의합니다.
- 예: mybatis.mapper-locations, mybatis.configuration.map-underscore-to-camel-case와 같은 설정은 Spring Boot가 MyBatis를 구성할 때 사용됩니다.
- 우선순위
- Spring Boot는 application.yml이나 application.properties에 정의된 설정을 우선적으로 사용하며, mybatis-config.xml의 설정은 명시적으로 Spring Boot에 통합되지 않는 경우 무시될 수 있습니다.
- 그러나, Spring Boot MyBatis Starter에서 mybatis.config-location을 통해 mybatis-config.xml의 경로를 지정하면 해당 파일의 설정도 로드됩니다.
mybatis-config.xml를 활성화하는 방법
application.yml에서 다음과 같이 mybatis.config-location을 지정하여 mybatis-config.xml 파일의 설정이 적용되도록 할 수 있습니다.
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:mappers/*.xml
나의 경우
mybatis-config.xml 에 아래처럼 cammel 설정이 되어 있었다.
<?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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
그리고 application.yml 에는 아래와 같이 mapper.xml 이 포함된 폴더 경로가 어디인지에 대한 설정이 되어 있었다.
mybatis:
mapper-locations: classpath:mappers/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
위에 글처럼 mybatis-config.xml 이 어디에 있으니 해당 파일을 loading 하라는 구문은 없었다.
설정의 일원화를 위해 mybatis-config.xml을 삭제 하고
아래처럼 applicaiton.yml 에 cammel 관련 설정까지 추가했다.
그후 cammel 로 oracle column의 '_' 가 바뀌어 들어가는걸 확인 했다.
최종 내용
mybatis:
mapper-locations: classpath:mappers/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true'SI 업무 > spring boot' 카테고리의 다른 글
| @RequestMapping 과 @GetMapping 의 중복 / (0) | 2024.12.26 |
|---|---|
| @Configuration 의 용도? Interface 를 어떻게 주입하지? (0) | 2024.12.26 |
| 5. JWT, Spring Security (0) | 2024.12.02 |
| 4. profile 로 다른 설정 관리 (0) | 2024.11.29 |
| 3. log는 어떻게 남기지? (1) | 2024.11.29 |