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>