Springboot+mybatis多数据源配置

applicationContext.xml具体配置如下

注意事项数据源扫描 mapper的文件夹路径不能重叠,否则会出现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" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


    <!-- 连接池配置 -->
    <bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
          init-method="init" destroy-method="close" abstract="true">
        <property name="xaDataSourceClassName" value="org.postgresql.xa.PGXADataSource"
        />  <!-- SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL,
            Oracle, PostgreSQL, Sybase, Hana] -->
        <!--   value="org.postgresql.xa.PGXADataSource" -->
        <property name="minPoolSize" value="10" />
        <property name="maxPoolSize" value="100" />
        <property name="borrowConnectionTimeout" value="30" />
        <property name="testQuery" value="select 1"/>
        <property name="maintenanceInterval" value="60" />
    </bean>



    <!-- 数据源A配置开始 -->
    <bean id="mainDataSource" primary="true" parent="abstractXADataSource">
        <property name="uniqueResourceName" value="db_main" />
        <property name="xaProperties">
            <props>
                   <!--		以下配置为  -->
                <prop key="url">数据库连接地址</prop>
                <prop key="user">账号</prop>
                <prop key="password" >密码</prop>
            </props>
        </property>
    </bean>

    <bean id="mainSqlSessionFactory" primary="true" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="mainDataSource" />
        <property name="configLocation" value="classpath:persistence/mybatis-config.xml" />
        <property name="mapperLocations">
            <array>
                <value>classpath*:persistence/xml/**/*.xml</value>
                <value>classpath*:/business/**/sqlxml/*.xml</value>
            </array>
        </property>
    </bean>

    <bean id="mainSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="mainSqlSessionFactory" />
    </bean>

    <!-- scan for mappers and let them be autowired -->
    <bean id="mainMapper" class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="ercp.**.persistence.mapper,ercp.business.**.mapper" />
        <property name="sqlSessionFactory" ref="mainSqlSessionFactory"/>
        <property name="markerInterface" value="tk.mybatis.mapper.common.Mapper" />
    </bean>
    <!-- 数据源A配置结束 -->




    <!-- 数据源B配置开始 -->
    <bean id="dlDataSource" primary="true" parent="abstractXADataSource">
        <property name="uniqueResourceName" value="pgsql-dl" />
        <property name="xaProperties">
            <props>
                <!--		以下配置为  -->
                <prop key="url">数据库连接地址</prop>
                <prop key="user">账号</prop>
                <prop key="password" >密码</prop>

            </props>
        </property>
    </bean>

    <bean id="dlSqlSessionFactory" primary="true" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dlDataSource" />
        <property name="configLocation" value="classpath:dldbpersistence/mybatis-config.xml" />
        <property name="mapperLocations" value="classpath*:dldbpersistence/xml/*.xml" />
    </bean>

    <bean id="dlSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="dlSqlSessionFactory"/>
    </bean>

    <!-- scan for mappers and let them be autowired -->
    <bean id="dlSqlMapper" class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="ercp.dldbpersistence.mapper" />
        <property name="sqlSessionFactory" ref="dlSqlSessionFactory"/>
        <property name="markerInterface" value="tk.mybatis.mapper.common.Mapper" />
    </bean>
    <!-- 数据源b配置结束 -->




</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值