MyBatis 可以配置成適應(yīng)多種環(huán)境载庭,這種機(jī)制有助于將 SQL 映射應(yīng)用于多種數(shù)據(jù)庫(kù)之中, 現(xiàn)實(shí)情況下有多種理由需要這么做廊佩。例如囚聚,開(kāi)發(fā)、測(cè)試和生產(chǎn)環(huán)境需要有不同的配置标锄;或者共享相同 Schema 的多個(gè)生產(chǎn)數(shù)據(jù)庫(kù)顽铸, 想使用相同的 SQL 映射。許多類似的用例料皇。
不過(guò)要記孜剿伞:盡管可以配置多個(gè)環(huán)境,每個(gè) SqlSessionFactory 實(shí)例只能選擇其一践剂。
所以毒返,如果你想連接兩個(gè)數(shù)據(jù)庫(kù),就需要?jiǎng)?chuàng)建兩個(gè) SqlSessionFactory 實(shí)例舷手,每個(gè)數(shù)據(jù)庫(kù)對(duì)應(yīng)一個(gè)。而如果是三個(gè)數(shù)據(jù)庫(kù)劲绪,就需要三個(gè)實(shí)例男窟,依此類推盆赤,記起來(lái)很簡(jiǎn)單:
每個(gè)數(shù)據(jù)庫(kù)對(duì)應(yīng)一個(gè) SqlSessionFactory 實(shí)例
所以在使用mybatis + spring時(shí),應(yīng)配置兩個(gè)數(shù)據(jù)源(datasource),每個(gè)數(shù)據(jù)源對(duì)應(yīng)一個(gè)SqlSessionFactory歉眷,然后為不同的SqlSessionFactory配置不同的
<mybatis:scan base-package ../>
即可牺六,要注意的的是,base-package
范圍小的需要放在配置文件前面
例子:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSourceChain" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${ac.jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${ac.chain.jdbc.url}"/>
<property name="user" value="${ac.chain.jdbc.username}"/>
<property name="password" value="${ac.chain.jdbc.password}"/>
<property name="minPoolSize" value="${ac.jdbc.miniPoolSize}"/>
<property name="maxPoolSize" value="${ac.jdbc.maxPoolSize}"/>
<property name="initialPoolSize" value="${ac.jdbc.initialPoolSize}"/>
<property name="maxIdleTime" value="${ac.jdbc.maxIdleTime}"/>
<property name="acquireIncrement" value="${ac.jdbc.acquireIncrement}"/>
<property name="acquireRetryAttempts" value="${ac.jdbc.acquireRetryAttempts}"/>
<property name="acquireRetryDelay" value="${ac.jdbc.acquireRetryDelay}"/>
<property name="testConnectionOnCheckin" value="${ac.jdbc.testConnectionOnCheckin}"/>
<property name="automaticTestTable" value="${ac.jdbc.automaticTestTable}"/>
<property name="idleConnectionTestPeriod" value="${ac.jdbc.idleConnectionTestPeriod}"/>
<property name="checkoutTimeout" value="${ac.jdbc.checkoutTimeout}"/>
</bean>
<!--- 第二個(gè)數(shù)據(jù)源 -->
<bean id="dataSourceApp" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${ac.jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${ac.app.jdbc.url}"/>
<property name="user" value="${ac.app.jdbc.username}"/>
<property name="password" value="${ac.app.jdbc.password}"/>
<property name="minPoolSize" value="${ac.jdbc.miniPoolSize}"/>
<property name="maxPoolSize" value="${ac.jdbc.slave.maxPoolSize}"/>
<property name="initialPoolSize" value="${ac.jdbc.initialPoolSize}"/>
<property name="maxIdleTime" value="${ac.jdbc.maxIdleTime}"/>
<property name="acquireIncrement" value="${ac.jdbc.acquireIncrement}"/>
<property name="acquireRetryAttempts" value="${ac.jdbc.acquireRetryAttempts}"/>
<property name="acquireRetryDelay" value="${ac.jdbc.acquireRetryDelay}"/>
<property name="testConnectionOnCheckin" value="${ac.jdbc.testConnectionOnCheckin}"/>
<property name="automaticTestTable" value="${ac.jdbc.automaticTestTable}"/>
<property name="idleConnectionTestPeriod" value="${ac.jdbc.idleConnectionTestPeriod}"/>
<property name="checkoutTimeout" value="${ac.jdbc.checkoutTimeout}"/>
</bean>
<mybatis:scan base-package="com.petkit.ac.adaptor.mapper.chain" factory-ref="mybatisSqlSessionFactoryChain" />
<bean id="mybatisSqlSessionFactoryChain" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceChain"/>
<property name="configLocation" value="classpath:/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:/config/mapper/chain/*.xml"/>
</bean>
<mybatis:scan base-package="com.petkit.ac.adaptor.mapper.app" factory-ref="mybatisSqlSessionFactoryApp" />
<bean id="mybatisSqlSessionFactoryApp" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceApp"/>
<property name="configLocation" value="classpath:/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:/config/mapper/app/*.xml"/>
</bean>
</beans>