DB와 java에서 사용하고자 하는 데이터 표기법이 다른경우.
snake 표기법 -> userId // DB에는 컬럼명으로 스네이크형식을 사용.
camelCase 표기법 -> user_id // 하지만 서버에서 객체의 멤버 변수의 이름은 카멜케이스 사용.
그렇기 때문에 MyBatis에서 resultType을 VO, DTO로 지정한다면 변수명이 달라서 데이터를 못받아오는 경우가 생긴다.
alias를 쿼리문마다 주거나 (SELECT user_id AS userId FROM DUAL),
resultMap을 쿼리에서 쓰는 컬럼 수 만큼 지정해주면 되긴 하지만
(원래는 xml에서 <resultMap/> 을 사용해서 일일이 매치해줬는데
솔직히 중노가다에 가깝고 귀찮았음)
snake 형태를 dto에서 사용하는 camelCase로 자동 변환 해주는 방법이 없을까?
찾아낸 방법은 이것(spring boot 기준)
mybatis.configuration.map-underscore-to-camel-case=true
1. application-properties에 아래 설정 추가
mybatis.configuration.map-underscore-to-camel-case=true
.yml
mybatis:
configuration: map-underscore-to-camel-case: true
설정은 추가해줬고,그러면 xml에서 어떻게 사용하는지 알아야한다.
2. xml파일에서 매핑하는법
testMapper.java의 경로는 test.xml의 namespace에 정의되어 있고,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.admin.mapper.testMapper">
<select id="getTestData" resultType="categoryDto">
SELECT user_id, category_name
FROM category
</select>
</mapper>
이 글에서는 Mapper클래스의 코드를 생략했지만, 쿼리의 id가 Mapper.java의 메소드 명과 일치 하는 것을 확인할 수있다.
Q. 그러면 xml의 resultType에서 쿼리의 결과와 dto는 어떻게 연결하지?
The fully qualified class name or alias for the expected type that will be returned from this statement. Note that in the case of collections, this should be the type that the collection contains, not the type of the collection itself. Use resultType OR resultMap, not both.
resultType에는 해당 dto, 혹은 vo 클래스의 경로를 지정해주면 된다.
(사실 그냥 dto가 있는 풀경로를 적어주면 되는데,
번거롭기 때문에 보통 application-properties와 같은 설정 파일에 dto 경로를 적어준 후 사용한다.)
application-properties 로 돌아가서 설정한 내용을 확인하자.
#appliction.properties
#mybatis 설정
mybatis.type-aliases-package=com.enter.modules.**.dto #dto 파일경로
mybatis.mapper-locations=/mapper/**/*.xml #mapper 파일경로
mybatis.configuration.map-underscore-to-camel-case=true
Mapper.java의 경우 @Mapper 어노테이션을 통해서 이 객체가 myBatis Mapper라는 것을 알려준다.
하지만 dto, xml 파일에 특별한 어노테이션이나 정보가 들어가지 않는다.
이 설정을 위해서 application-properties에 dto파일경로, mapper 파일경로와 같은 mybatis 설정을 추가해주는 것이다.
mybatis.type-aliases-package 는 설정하지 않아도 되지만, 추가하지 않으면 xml파일에서 dto를 사용시 resultType에 풀경로를 넣어줘야 한다. => (resultType ="com.enter.moules.category.categoryDto" 이런식으로 해당경로 전부 지정.)
mybatis.type-aliases-package 로 dto 파일 경로를 설정 하면 resultType에서는 Dto만 명시해서 간단하게 쓸 수 있다.
** 으로 중간 경로를 all로 잡아줬기 때문에 dto를 항목별로 분류해서 사용하는 경우에도 사용 가능하다.
resultType="categoryDto"
위와 같이 mybatis.type-aliases-package=com.enter.modules.**.dto 로 dto파일의 경로를 설정해 주었기 때문에,
쿼리문에서 resultType에는 dto명만 적어주면 간단히 끝난다.
<select id="getTestData" resultType="categoryDto">
// 쿼리문 생략
</select>
참고자료 : https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
mybatis-spring-boot-autoconfigure – Introduction
Introduction What is MyBatis-Spring-Boot-Starter? The MyBatis-Spring-Boot-Starter help you build quickly MyBatis applications on top of the Spring Boot. By using this module you will achieve: Build standalone applications Reduce the boilerplate to almost z
mybatis.org
'BackEnd > MyBatis' 카테고리의 다른 글
[MyBatis] list로 넘긴 데이터를 foreach 로 where in 구문에서 사용 (0) | 2022.09.15 |
---|---|
[MyBatis] resultMap (0) | 2020.11.16 |
[MyBatis] 반복되는 쿼리 묶기_<sql/><include/> (0) | 2020.08.31 |