首页 > 数据库技术 > 详细

Spring-mybatis 配置两个或多个数据库源

时间:2020-01-06 16:44:26      阅读:85      评论:0      收藏:0      [点我收藏+]

在实际应用场景中会遇到一个项目连接多个数据库的情况,下面来做一个简单的配置。

作者在开发过程中把数据库的配置放在了properties文件中,如图:

技术分享图片

spring-mybatis配置放在了了spring-dao.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:context="http://www.springframework.org/schema/context" 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.2.xsd

            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >

<property name="driverClass" value="${jdbc.driver}" />

<property name="jdbcUrl" value="${jdbc.url}" />

<property name="user" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

<property name="maxPoolSize" value="30"/>

<property name="minPoolSize" value="10"/>

<property name="autoCommitOnClose" value="false"/>

<property name="checkoutTimeout" value="10000"/>

<property name="acquireRetryAttempts" value="2"/>

</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<property name="configLocation" value="classpath:mybatis-config.xml"/>

<property name="typeAliasesPackage" value="com.senxing.po"/>

<property name="mapperLocations" value="classpath:mapper/*xml"/>

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

<property name="basePackage" value="com.senxing.dao"/>

</bean>

<!--第二个数据源配置-->

<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" >

<property name="driverClass" value="${jdbc.test.driver}" />

<property name="jdbcUrl" value="${jdbc.test.url}" />

<property name="user" value="${jdbc.test.username}" />

<property name="password" value="${jdbc.test.password}" />

<property name="maxPoolSize" value="30"/>

<property name="minPoolSize" value="10"/>

<property name="autoCommitOnClose" value="false"/>

<property name="checkoutTimeout" value="10000"/>

<property name="acquireRetryAttempts" value="2"/>

</bean>

<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource2"/>

<property name="configLocation" value="classpath:mybatis-config.xml"/>

<property name="typeAliasesPackage" value="com.senxing.po"/>

<property name="mapperLocations" value="classpath:mapper/backdao/*xml"/>

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>

<property name="basePackage" value="com.senxing.backdao"/>

</bean>

</beans>

如上所示,我们分别配置了两个 dataSource,两个sqlSessionFactory,关键的地方在于MapperScannerConfigurer 的配置——使用sqlSessionFactoryBeanName属性,注入不同的sqlSessionFactory的名称,这样的话,就为不同的数据库对应的 mapper 接口注入了对应的 sqlSessionFactory。

现在就可以用了

Spring-mybatis 配置两个或多个数据库源

原文:https://www.cnblogs.com/gaoweifeng/p/12156614.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!