Druid中配置双数据库

配置如下

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.2.xsd"
    default-autowire-candidates="byType">

<!--  
单数据源 
 -->
 <!-- 
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="driverClassName">
            <value>${dbDriver}</value>
        </property>
        <property name="url">
            <value>${dbUrl}</value>
        </property>
        <property name="username">
            <value>${dbUsername}</value>
        </property>
        <property name="password">
            <value>${dbPassword}</value>
        </property>
        <property name="maxActive">
            <value>${maxActive}</value>
        </property>
        <property name="initialSize">
            <value>${initialSize}</value>
        </property>
        <property name="maxWait">
            <value>${maxWait}</value>
        </property>
        <property name="minIdle">
            <value>${minIdle}</value>
        </property>
        <property name="removeAbandoned">
            <value>${removeAbandoned}</value>
        </property>
        <property name="removeAbandonedTimeout">
            <value>${removeAbandonedTimeout}</value>
        </property>
        <property name="connectionProperties">
            <value>${connectionProperties}</value>
        </property>
        <property name="logAbandoned" value="true" />
    </bean>
    
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSource" p:configLocation="classpath:common/mybatis-config.xml"
        p:mapperLocations="classpath*:common/mapper/**/*.xml" />
         -->
    <!-- mybatis.spring自动映射 -->
    <!--
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.niko.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />

    <tx:annotation-driven transaction-manager="transactionManager" />
     -->
     
     <!-- 多数据源 mysql 和 sqlserver 共存 -->
     
    <bean id="sqlServerDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.sqlserver.driver}"/>
        <property name="url" value="${jdbc.sqlserver.url}"/>
        <property name="username" value="${jdbc.sqlserver.username}"/>
        <property name="password" value="${jdbc.sqlserver.password}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
        <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
        <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
        <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
        <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
        <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>
        <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
    </bean>
    <bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.mysql.driver}"/>
        <property name="url" value="${jdbc.mysql.url}"/>
        <property name="username" value="${jdbc.mysql.username}"/>
        <property name="password" value="${jdbc.mysql.password}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
        <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
        <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
        <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
        <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
        <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>
        <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
    </bean>
    
    
    <bean id="sqlserverSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" 
        p:dataSource-ref="sqlServerDataSource"
        p:configLocation="classpath:common/mybatis-config.xml"
        p:mapperLocations="classpath*:common/mapper/**/*.xml">
    </bean>
    
       <bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" 
           p:dataSource-ref="mySqlDataSource"
        p:configLocation="classpath:common/mybatis-config.xml"
        p:mapperLocations="classpath*:common/mapper/**/*.xml">
    </bean>
    

    <!-- mybatis.spring自动映射 -->
    <bean id="sqlserverMSF"  class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.niko.dao.sqlserver" p:sqlSessionFactoryBeanName="sqlserverSessionFactory" />
        
    <bean id="mysqlMSF"  class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.niko.dao.mysql" p:sqlSessionFactoryBeanName="mysqlSessionFactory" />

    <bean id="transactionManagerSqlserver"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="sqlServerDataSource" />

    <bean id="transactionManagerMysql"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="mySqlDataSource" />
        
    <tx:annotation-driven transaction-manager="transactionManagerSqlserver" />
    <tx:annotation-driven transaction-manager="transactionManagerMysql" />

</beans>
原文地址:https://www.cnblogs.com/wingfay/p/8990391.html