본문 바로가기

백엔드/Spring

Spring 3.2.3 + myBatis 3.2.2


기존에 사용하던 Spring 3.1에서 3.2.3으로 변경했다. 하는김에 myBatis도 최신으로 변경.. 
이러다가 그냥 back-end를 다 최신으로 변경하기로 작정하고 작업 진행..

applicationContext.xml 에서 myBatis 설정이 조금 변경되었다. 

org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
as of Spring 3.2, in favor of the native Spring support in the Mybatis follow-up project (http://code.google.com/p/mybatis/)
org.springframework.orm.ibatis.SqlMapClientFactoryBean
as of Spring 3.2, in favor of the native Spring support in the Mybatis follow-up project (http://code.google.com/p/mybatis/)
org.springframework.orm.ibatis.SqlMapClientTemplate
as of Spring 3.2, in favor of the native Spring support in the Mybatis follow-up project (http://code.google.com/p/mybatis/)

3.2 버전 이전은 위 클래스를 이용하여 myBatis 설정을 했지만 3.2 이후부터는 불가능하다.

> myBatis configuration codes in applicationContext
1
2
3
4
5
6
7
8
9
10
<!-- myBATIS 3.2.3 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:resources/SqlMapConfig.xml"></property>
    <property name="mapperLocations" value="classpath*:com/openerp/**/*-mapper.xml"></property>
    <!-- not use this transactionFactory because use spring transactionFactory  -->
    <property name="transactionFactory">
        <bean class="org.mybatis.spring.transaction.SpringManagedTransactionFactory"></bean>
    </property>
</bean>

심플하다... SqlMapConfig에 mapper를 일일이 적어주어도 되지만
mapperLocations를 이용하여 패키지 검색으로 자동 매핑되게도 할 수 있고 이게 더 편하다.

또 하나, 기본 설정 그대로 사용하고자 한다면 configLocation을 적지 않아도 된다.

아래는 maven에서의 myBatis 설정에 관련된 코드.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<properties>
    <!-- my baits -->
    <org.mybatis.version>3.2.2</org.mybatis.version>
    <org.mybatis.spring.version>1.2.0</org.mybatis.spring.version>
    <!-- my baits -->
</properties>
 
<dependencies>
    <!-- my batis -->
    <dependency>
        <groupid>org.mybatis</groupid>
        <artifactid>mybatis</artifactid>
        <version>${org.mybatis.version}</version>
    </dependency>
    <dependency>
        <groupid>org.mybatis</groupid>
        <artifactid>mybatis-spring</artifactid>
        <version>${org.mybatis.spring.version}</version>
    </dependency>
</dependencies>