티스토리 뷰

728x90

다중디비 설정은 간단한데 아주 중요한..부분이 있다. 이 부분을 놓치면...몇시간동안 뻘짓 할 수도 있으니...꼭 확인하기!

 

0️⃣ DB dependency

- 오라클은 maven에서 정식으로 지원하지 않기 때문에 아래처럼 repository를 설정해줘야 드라이버를 다운받을 수 있다

- DB버전 : 오라클 11g , mariaDB 10.6

<!--oracle 라이브러리를 받기 위해 repository 설정-->
<repositories>
	<repository>
		<id>oracle</id>
		<name>ORACLE JDBC Repository</name>
		<url>http://www.datanucleus.org/downloads/maven2/</url>
	</repository>
</repositories>

<!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc8 -->
	<dependency>
		<groupId>com.oracle.ojdbc</groupId>
		<artifactId>ojdbc8</artifactId>
		<version>19.3.0.0</version>
	</dependency>

	<dependency>
		<groupId>org.mariadb.jdbc</groupId>
		<artifactId>mariadb-java-client</artifactId>
		<version>2.6.0</version>
	</dependency>

1️⃣ 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"
	   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">

	<!--값을 globals.properties에서 가져오기 위한 설정 -->
	<bean id="propertyConfigurer"
		  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:/egovframework/egovProps/globals.properties</value>
			</list>
		</property>
	</bean>

	<!--datasource 설정(propertyConfigurer 활용) -->
	<alias name="dataSource-${Globals.DbType}" alias="dataSource" />
	<alias name="dataSource-${Globals.oracle.DbType}" alias="oracle.dataSource"/>

	<!--mysql -->
	<bean id="dataSource-mysql" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${Globals.DriverClassName}"/>
		<property name="url" value="${Globals.Url}" />
		<property name="username" value="${Globals.UserName}"/>
		<property name="password" value="${Globals.Password}"/>
		<property name="validationQuery" value="SELECT 1" />
	</bean>
    
	<!-- Oracle -->
	<bean id="dataSource-oracle" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${Globals.DriverClassName}"/>
		<property name="url" value="${Globals.oracle.Url}" />
		<property name="username" value="${Globals.oracle.UserName}"/>
		<property name="password" value="${Globals.oracle.Password}"/>
		<property name="validationQuery" value="SELECT 1 from dual" />
	</bean>

</beans>

 

2️⃣ mapper 설정

⭐ 여기서 중요한 점은 basePackage를 구분해줘야한다⭐

- 나는 처음에 mysql의 basePackage를 com.view.**.mapper 

oracle의 basePackage를 com.view.oracle.mapper 이런식으로 해서..4시간 동안 삽질했다.

이런식으로 구분하면 안되고, 아예 다른 패키지로 철저하게 분리해줄 것!

예) mysql의 basePackage : com.view.**.mapper | oracle의 basePackage : com.system.**.mapper

<?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-4.0.xsd">

	<bean class="egovframework.rte.psl.dataaccess.mapper.MapperConfigurer">
		<property name="basePackage" value="com.view.**.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSession"/>
	</bean>
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache"  primary="true">
		<constructor-arg index="0" ref="sqlSession" />
	</bean>

	<bean id="oracleSqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource-oracle"/>
		<property name="mapperLocations" value="classpath:/egovframework/sqlmap/mappers/oracle/*.xml"/>
		<property name="configLocation" value="classpath:/egovframework/sqlmap/config/oracle-mapper-config.xml" />
	</bean>
	<bean class="egovframework.rte.psl.dataaccess.mapper.MapperConfigurer">
		<property name="basePackage" value="com.system.mapper"/>
		<property name="sqlSessionFactoryBeanName" value="oracleSqlSession"/>
	</bean>
	<bean id="oracle.sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
		<constructor-arg index="0" ref="oracleSqlSession" />
	</bean>

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