> Hello World !!!

     

@syaku

Spring Mybatis 다중 연결 맵퍼 스캔하기 : Multiple Datasource Mapper scan

written by Seok Kyun. Choi. 최석균

Spring Mybatis 다중 연결 맵퍼 스캔하기 : Multiple Datasource Mapper scan

두개이상의 데이터베이스를 연결하고 각각에 맞는 맵퍼를 스캔하는 방법을 설명한다.
이미 한개의 datasource 가 설정되어 있다는 가정하에 예제를 설명한다.

새로운 데이터베이스 연결을 위한 datasource 설정 파일을 만든다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    ">

    <alias name="twoDataSource" alias="two.dataSource"/>

    <bean id="twoDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.tmax.tibero.jdbc.TbDriver" />        
        <property name="url" value="xx" /> 
        <property name="username" value="xx" />
        <property name="password" value="xx" /> 
        <property name="initialSize" value="1" />
        <property name="maxActive" value="5" />
        <property name="poolPreparedStatements" value="true" />
        <property name="maxOpenPreparedStatements" value="10" />        
        <property name="validationQuery" value="select 1 from dual" />
    </bean>
</beans>

datasource 만들때 기존의 datasource 설정의 빈 네임과 중복되지 않게 지정한다.

mybatis 맵퍼 스캔을 위한 설정 파일을 만든다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    ">

    <bean id="twoSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="two.dataSource" />
    <property name="configLocation" value="classpath:mybatis-config-base.xml" />
    <property name="mapperLocations">
    <list>    <value>classpath*:org/syaku/mapper/dataMapper.two.tibero.xml</value>    
    </list>
    </property>
    </bean>

    <bean class="egovframework.rte.psl.dataaccess.mapper.MapperConfigurer">
        <property name="basePackage" value="org.syaku" />
        <property name="annotationClass" value="org.syaku.annotation.TwoMapperScan" />
        <property name="sqlSessionFactoryBeanName" value="eduSqlSessionFactory" />
    </bean>

</beans>

기존의 mybatis 설정과 크게 다르지 않다. 하단에 인터페이스 맵퍼를 스캔하기 위한 설정에서 어노테이션 설정이 추가되었다.
그리고 위 설정은 전자정부프레임워크를 이용한 것이고 일반적인 spring mybatis 는 아래와 같이 한다.

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.syaku" />
    <property name="annotationClass" value="org.syaku.annotation.TwoMapperScan" />
    <property name="sqlSessionFactoryBeanName" value="eduSqlSessionFactory" />
</bean>

마지막으로 맵퍼 스캔을 위한 어노테이션을 추가한다. 이미 첫번째 datasource 에서 맵퍼용 어노테이션을 사용했기 때문에 사용하면 맵퍼의 스테이트먼트를 찾을 수 없다는 오류가 발생할 것이다. 그래서 새로운 맵퍼 스캔용 어노테이션을 아래와 같이 생성한다. (2개 이상 연결이 필요할때 필요한 만큼 새로 만들면 된다.)

package org.syaku.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.stereotype.Component;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface TwoMapperScan {
    String value() default "";
}

이제 myBatis 인터페이스 맵퍼가 스캔될 수 있게 어노테이션을 추가한다.

package org.syaku.mapper;

import egovmei.resources.edu.annotation.EduMapperScan;

@TwoMapperScan
public interface TwoEgovMapper {

}

posted syaku blog

Syaku Blog by Seok Kyun. Choi. 최석균.

http://syaku.tistory.com