spring + mybatis多数据源配置

本文详细介绍了如何在Spring框架下配置多数据源,并通过实际XML文件展示配置过程,包括数据源创建、SqlSessionFactory、事务管理器和MapperScannerConfigurer等关键组件的配置。同时,文章还提供了一个完整的配置示例,帮助开发者理解和实现多数据源的应用。
摘要由CSDN通过智能技术生成

网上说的配置不太准确,主要问题出现在这里:

<!-- 自动扫描 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" />

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值