Preloader image
DDD

자바

DB 2중 접속 XML 설정

작성자 관리자 (admin)
조회수 273
입력일 2022-08-17 13:53:33

<?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:p="http://www.springframework.org/schema/p"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    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
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx.xsd 
                http://www.springframework.org/schema/jee
                http://www.springframework.org/schema/jee/spring-jee.xsd">

    <!-- multiple data source & sqlSessionFactory -->
    <bean id="ds-one" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@//db1.example.com:1521/ocrl" />
        <property name="username" value="user" />
        <property name="password" value="userpwd" />
    </bean>
    <bean id="ds-two" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />       
        <property name="url" value="jdbc:mysql://db2.example.com:3306/lesstif?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="username" value="userid" />
        <property name="password" value="useriduserPwd" />
    </bean>

    <bean id="dsOneSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:mapperLocations="classpath:/com/example/mapper-one/*mapper.xml"
        p:configLocation="classpath:/com/example/dsone-mybatis-config.xml" 
        p:dataSource-ref="ds-one" />

    <bean id="dsTwoSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:mapperLocations="classpath:/com/example/mapper-two/*mapper.xml"
        p:configLocation="classpath:/com/example/dstwo-mybatis-config.xml" 
        p:dataSource-ref="ds-two" />
      
    <bean id="dsOneScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.example.mapper-one" />
 
    <bean id="dsTwoScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.example.mapper-two" />

    <bean id="dsOnetransactionManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"    p:dataSource-ref="ds-one" />
    <bean id="dsTwotransactionManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"    p:dataSource-ref="ds-two" />

    <context:component-scan base-package="com.example.service-one, com.example.service-two">
    </context:component-scan>
</beans>

^