티스토리 뷰

DeVeLoP

spring MVC - 다중 DB 연결

freecatz 2022. 11. 23. 16:56

mybatis-spring-1.2.4 이상 사용을 권장

이하의 버젼에서는 Bean creation exception on non-lazy FactoryBean type check ... 메세지가 나타난다.

 

context-datasource.xml 파일 수정

	<!-- 첫번째 데이터베이스 관련 설정 -->
	<bean id="mysqlDataSource" class="com.zaxxer.hikari.HikariDataSource">
		<property name="driverClassName" value="${mysql.datasource.driver}" />
		<property name="jdbcUrl" value="${mysql.datasource.url}" />
		<property name="username" value="${mysql.datasource.username}" />
		<property name="password" value="${mysql.datasource.password}" />
		<property name="maximumPoolSize" value="${mysql.datasource.maximumpoolsize}" />
	</bean>
	<bean id="mysqlSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="mysqlDataSource" />
		<property name="configLocation" value="classpath:config/mysql-config.xml" />
		<property name="typeAliasesPackage" value="kr.pe.freecatz.mysql" />
	</bean>
	<bean id="mysqlSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="mysqlSqlSessionFactory" />
		<constructor-arg index="1" value="BATCH"/>
	</bean>
	<bean id="mysqlSqlScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="kr.pe.freecatz.mysql" />
		<property name="sqlSessionFactoryBeanName" value="mysqlSqlSessionFactory" />
	</bean>
	<bean id="mysqlTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="mysqlDataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="mysqlTransactionManager"/>
	
	
	<!-- 두번째 데이터베이스 관련 설정 -->
	<bean id="oracleDataSource" class="com.zaxxer.hikari.HikariDataSource">
		<property name="driverClassName" value="${oracle.datasource.driver}" />
		<property name="jdbcUrl" value="${oracle.datasource.url}" />
		<property name="username" value="${oracle.datasource.username}" />
		<property name="password" value="${oracle.datasource.password}" />
		<property name="maximumPoolSize" value="${oracle.datasource.maximumpoolsize}" />
	</bean>
	<bean id="oracleSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="oracleDataSource" />
		<property name="configLocation" value="classpath:config/oracle-config.xml" />
		<property name="typeAliasesPackage" value="kr.pe.freecatz.oracle" />
	</bean>
	<bean id="oracleSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="oracleSqlSessionFactory" />
		<constructor-arg index="1" value="BATCH"/>
	</bean>
		<bean id="oracleSqlScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="kr.pe.freecatz.oracle" />
		<property name="sqlSessionFactoryBeanName" value="oracleSqlSessionFactory" />
	</bean>
	<bean id="oracleTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="oracleDataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="oracleTransactionManager"/>

 

 

sqlSessionFactoryBeanName 속성을 이용하여 @Resource 어노테이션을 작성 하지 않아도 패키지로 구분 하여 데이터베이스 접근이 가능 하다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
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
글 보관함