티스토리 뷰

MyBatis 진영에서 MyBatis-Spring-Boot-Starter 라는것을 내놓았다. 아니.. 나만 몰랐던것 같다. 누군가가 작성한 샘플을보니 mybatis-config.xml (MyBatis 설정파일) 파일에 보통 있어야 할 내용들이 없는데도 있는것처럼 동작을 하는것이 의아하게 느껴져서 찾아보니 application.yml 파일에 기술이 되어 있었다. application.yml을 통해 기존에 mybatis-config.xml에서 사용하던 기능을 전부 다 사용할 수 있는것은 아니고 더 적은 xml 구성정도 지원을 해준다고 보면 된다. 

이를 어떻게 설정하고 사용하는지 알아보자. 


pom.xml

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

가장먼저 해야 할 일은 의존성을 추가해주는 일이다. mybatis-spring-boot-starter 의존성을 추가해주면 한방에 MyBatis를 활용할 수 있다. 추가해줄때 주의사항은 mybatis-spring-boot-starter와 springboot의 버전이 호환이 되는지 확인을 해줘야 한다. 

MyBatis-Spring-Boot-Starter version mybatis-spring springboot version java version
2.1 2.0 (모든 기능을 활성화하려면 2.0.2 이상 필요) 2.1 이상 8 이상
2.0 (EOL) 2.0 2.0 또는 2.1 8 이상
1.3 1.3 1.5 6 이상
1.2 (EOL) 1.3 1.4 6 이상
1.1 (EOL) 1.3 1.3 6 이상
1.0 (EOL) 1.2 1.3 6 이상

위의 표를 보고 본인의 springboot version, java version에 맞는 mybatis-spring-boot-starter를 껴 넣도록 하자. 

 

 

application.yml

mybatis:
  configuration:
    multiple-result-sets-enabled: false
    map-underscore-to-camel-case: true
    call-setters-on-nulls: true    
    jdbc-type-for-null: varchar
    default-fetch-size: 500
  mapper-locations:
  - classpath:mybatis/mapper/*.xml

application.yml에서는 위와 같은 모습으로 작성을 할 수 있다. 기존에 mybatis-config.xml (일반적으로 이렇게 많이 사용하는듯) 에서 작성했던 내용들인데 이곳에서 작성하던 항목들을 application.yml 파일에서 작성할 수 있다. 물론 application.properties 파일에서도 가능하다. 작성 가능한 properties는 다음과 같다. 

 

Property Description
config-location Location of MyBatis xml config file.
check-config-location Indicates whether perform presence check of the MyBatis xml config file.
mapper-locations Locations of Mapper xml config file.
type-aliases-package Packages to search for type aliases. (Package delimiters are “,; \t\n”)
type-aliases-super-type The super class for filtering type alias. If this not specifies, the MyBatis deal as type alias all classes that searched from type-aliases-package.
type-handlers-package Packages to search for type handlers. (Package delimiters are “,; \t\n”)
executor-type Executor type: SIMPLE, REUSE, BATCH
default-scripting-language-driver The default scripting language driver class. This feature requires to use together with mybatis-spring 2.0.2+.
configuration-properties Externalized properties for MyBatis configuration. Specified properties can be used as placeholder on MyBatis config file and Mapper file. For detail see the MyBatis reference page.
lazy-initialization Whether enable lazy initialization of mapper bean. Set true to enable lazy initialization. This feature requires to use together with mybatis-spring 2.0.2+.
mapper-default-scope Default scope for mapper bean that scanned by auto-configure. This feature requires to use together with mybatis-spring 2.0.6+.
configuration.* Property keys for Configuration bean provided by MyBatis Core. About available nested properties see the MyBatis reference page. NOTE: This property cannot be used at the same time with the config-location.
scripting-language-driver.thymeleaf.* Property keys for ThymeleafLanguageDriverConfig bean provided by MyBatis Thymeleaf. About available nested properties see the MyBatis Thymeleaf reference page.
scripting-language-driver.freemarker.* Properties keys for FreeMarkerLanguageDriverConfig bean provided by MyBatis FreeMarker. About available nested properties see the MyBatis FreeMarker reference page. This feature requires to use together with mybatis-freemarker 1.2.0+.
scripting-language-driver.velocity.* Properties keys for VelocityLanguageDriverConfig bean provided by MyBatis Velocity. About available nested properties see the MyBatis Velocity reference page. This feature requires to use together with mybatis-velocity 2.1.0+.

출처 : mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

 

가장 많이 사용되는 configuration.* 에 해당하는 부분은 mybatis.org/mybatis-3/configuration.html#settings 에서 확인할 수 있다. 이곳에서 CamelCase로 작성된 setting에 해당되는 항목을 application.yml 양식에 맞게 - 으로 구분지어서 사용하면 된다. 예를들면 mapUnderscoreToCamelCase는 map-underscore-to-camel-case로 작성하면 된다. 아니면 application.yml 파일에서 Ctrl + Space를 누르면 사용할 수 있는 항목들이 나오니 그것을 사용하면 된다. 이렇게 정의하고 남은 플러그인 정보 정도만 mybatis-config.xml 파일에 기술을 해주면 된다. 

 

끝!

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31