mybatis로 multiple DB에 접근할 일이 있는데. sqlSessionFactory를 여러 개를 두어서 관리하도록 에러 메시지가 나와서. MapperScannerConfigurer의 sqlSessionTemplateBeanName property를 추가하니 문제가 되었다.

properties 파일을 못읽는 부분, DAO를 못찾는 exception이 나온다.

 

* 사용 라이브러리

 

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.0</version>
        </dependency>


        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.0</version>
        </dependency>

 

 

 

* 사용 mybatis 연동 beans 설정

 

 

     <bean id="adminDataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${template.jdbc.url.master}" />
        <property name="username" value="${template.jdbc.username}" />
        <property name="password" value="${template.jdbc.password}" />
        ....
    </bean>

    <bean id="adminSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="adminDataSource" />
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    </bean>

    <bean id="adminTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="adminDataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="adminMapper"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="google.talk.admin.repository" />
        <property name="sqlSessionFactory" ref="adminSqlSessionFactory" />
    </bean>


그래서, 문서를 잘 보니. 아래와 같은 좋은 문구가 있었다.

 

http://www.mybatis.org/spring/mappers.html#MapperFactoryBean

 

Notice that there is no need to specify a SqlSessionFactory or SqlSessionTemplate because the MapperScannerConfigurer will create MapperFactoryBeans that can be autowired. But if you are using more than one DataSource autowire may not work for you. In this case you can use the sqlSessionFactoryBeanName or sqlSessionTemplateBeanName properties to set the right bean name to use. Note that bean names are required, not bean references, thus the value attribute is used instead of the usual ref:

 

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />



아래와 mapper의 property의 내용과 mybatis-spring을 업그레이드하니 문제가 없었다.

 

1) property 수정 (ref –> value)

   <bean id="adminMapper"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="google.talk.admin.repository" /> 

        <property name="sqlSessionFactoryBeanName" value="adminSqlSessionFactory" />
    </bean>

 

 

2) 1.1.1 최신버전으로 변경 (1.1.0 –> 1.1.1)

       <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>


Posted by '김용환'
,