网上说的配置不太准确,主要问题出现在这里:
<!-- 自动扫描 mybatis mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="oneSqlSessionFactory"></property>
<property name="basePackage" value="aaa.bbb.*.dao" />
</bean>
在单数据源情况下红色部分的可以不写,如果要配置多数据源,必须加上红色部分
注意是name="sqlSessionFactoryBeanName",而不是name="sqlSessionFactory"(错误)
下面附上完整的配置
数据源1,dataSource1.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
<bean id="oneDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${c3p0.driverClassName}"></property>
<property name="jdbcUrl" value="${one.c3p0.url}"> </property>
<property name="user" value="${one.c3p0.username}"> </property>
<property name="password" value="${one.c3p0.password}"> </property>
<property name="initialPoolSize" value="${one.c3p0.initialPoolSize}"></property>
<property name="maxPoolSize" value="${one.c3p0.maxPoolSize}"> </property>
<property name="minPoolSize" value="${one.c3p0.minPoolSize}"> </property>
<property name="acquireIncrement" value="${one.c3p0.acquireIncrement}"> </property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"> </property>
</bean>
<!-- mybatis -->
<bean id="oneSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="oneDataSource" />
<property name="configLocation">
<value>classpath:mybatis/mybatisConfig.xml</value>
</property>
<property name="mapperLocations">
<list>
<value>classpath:mybatis/one/*.xml</value>
</list>
</property>
</bean>
<bean id="oneTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="oneDataSource"/>
</bean>
<bean id="oneTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="oneTransactionManager"/>
</bean>
<!--inject dao list -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="oneSqlSessionFactory"></property>
<property name="basePackage" value="aaa.bbb.one.*.dao" />
</bean>
</beans>
数据源2,dataSource2.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
<bean id="twoDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${c3p0.driverClassName}"></property>
<property name="jdbcUrl" value="${two.c3p0.url}"> </property>
<property name="user" value="${two.c3p0.username}"> </property>
<property name="password" value="${two.c3p0.password}"> </property>
<property name="initialPoolSize" value="${two.c3p0.initialPoolSize}"></property>
<property name="maxPoolSize" value="${two.c3p0.maxPoolSize}"> </property>
<property name="minPoolSize" value="${two.c3p0.minPoolSize}"> </property>
<property name="acquireIncrement" value="${two.c3p0.acquireIncrement}"> </property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"> </property>
</bean>
<!-- mybatis -->
<bean id="twoSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="twoDataSource" />
<property name="configLocation">
<value>classpath:mybatis/mybatisConfig.xml</value>
</property>
<property name="mapperLocations">
<list>
<value>classpath:mybatis/two/*.xml</value>
</list>
</property>
</bean>
<bean id="twoTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="twoDataSource"/>
</bean>
<bean id="twoTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="twoTransactionManager"/>
</bean>
<!--inject dao list -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="twoSqlSessionFactory"></property>
<property name="basePackage" value="aaa.bbb.two.*.dao" />
</bean>
</beans>
其实只要依葫芦画瓢,把所有的bean都复制一份,然后名字变一下就可以了
然后在applicationContext.xml添加
<import resource="classpath*:dataSource1.xml" />
<import resource="classpath*:dataSource2.xml" />