Mybatis 同一个Mapper支持不同的数据源

单数据源配置方式

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="mapperLocations" value="classpath:com/mapper/*.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
	<property name="basePackage" value="com.mapper" /> 
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

多数据源配置方式_1

<bean id="dataSource_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="dataSource_2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sqlSessionFactory_1" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource_1" />
	<property name="mapperLocations" value="classpath:com/mapper/*.xml" />
</bean>

<bean id="sqlSessionFactory_2" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource_2" />
	<property name="mapperLocations" value="classpath:com/mapper/*.xml" />
</bean>

<bean id="multipleSqlSessionTemplate" class="com.*.*.MultipleSqlSessionTemplate">
	<constructor-arg index="0" ref="masterSqlSessionFactory" />
	<property name="targetSqlSessionFactorys">
		<map>  
			<entry value-ref="sqlSessionFactory_1" key="ssf_1"/>  
			<entry value-ref="sqlSessionFactory_2" key="ssf_2"/>  
		</map>  
	</property>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
	<property name="basePackage" value="com.mapper" /> 
	<property name="sqlSessionTemplateBeanName" value="multipleSqlSessionTemplate" />
</bean>

参考:https://www.cnblogs.com/FlyHeLanMan/p/6744171.html

多数据源配置方式_2

<bean id="dataSource_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="dataSource_2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="multipleDataSource" class="com.*.*.MultipleDataSource">
	<property name="defaultTargetDataSource" ref="dataSource_1" />
	<property name="targetDataSources">
		<map>
			<entry key="ds_1" value-ref="dataSource_1" />
			<entry key="ds_2" value-ref="dataSource_2" />
		</map>
	</property>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="multipleDataSource" />
	<property name="mapperLocations" value="classpath:com/mapper/*.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
	<property name="basePackage" value="com.mapper" /> 
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

参考:https://www.cnblogs.com/digdeep/p/4512368.html 

 

Mybatis 同一个Mapper支持不同的数据源

上一篇:【Java集合源码剖析】ArrayList源码剖析


下一篇:Python之路番外(第二篇):PYTHON基本数据类型和小知识点