在小型項目中,一般配置一個數據庫,也就是一個mybatis數據源,但是有時候需要同時支持兩種數據庫,比如mysql和oracle. 最笨的方法就是配置兩個spring配置文件,然后根據不同的部署,采用不同的配置文件,其實這兩個配置文件可以合成一個配置文件,通過java的properties文件進行配置。
首先說明下配置單個數據庫,也就是單個數據源的配置。
首先看一下spring的配置文件applicationContext.xml(這里采用的是spring+mybatis,所以有關數據庫的及mybatis的配置都在這里):
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.springframework.org/schema/aop classpath:/org/springframework/aop/config/spring-aop-3.0.xsd http://www.springframework.org/schema/context classpath:/org/springframework/context/config/spring-context-3.0.xsd http://www.springframework.org/schema/tx classpath:/org/springframework/transaction/config/spring-tx-3.0.xsd"> <!-- IoC配置 --> <!-- 掃描類包,將標注Spring注解的類自動轉化Bean,同時完成Bean的注入 --> <context:component-scan base-package="com.shr.dao" /> <context:component-scan base-package="com.shr.service" /> <!-- DAO配置 --> <context:property-placeholder location="classpath:config.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${userName}"/> <property name="password" value="${password}"/> </bean> <bean name="myBatisSQLInterceptor" class="com.shr.dao.MyBatisSQLInterceptor"></bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.shr.dao.pojo,com.shr.dao.model" /> <property name="mapperLocations"> <list> <value>classpath:com/shr/dao/resources/mappers/*_mapper.xml</value> </list> </property> <!-- <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/> --> <property name="typeHandlersPackage" value="com.shr.dao" /> <property name="plugins"> <list> <ref bean="myBatisSQLInterceptor"/> </list> </property> </bean> <!-- 配置事務管理器 --> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.shr.dao.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- <property name="markerInterface" value="com.shr.dao.mapper.ITemplateMapper"/> --> </bean> </beans>
properties配置文件內容為:
#mysql configuration driver=com.mysql.jdbc.Driver url=jdbc:mysql://10.10.193.111:3306/sp5000?useUnicode=true&characterEncoding=UTF-8 username=shr password=shr
通過properties配置文件可以看到我們所配的數據庫是mysql.
在applicationContext.xml文件中,我們只配置了一個數據源,就是“dataSource”,存放mybatis配置文件的地方為:
<property name="mapperLocations"> <list> <value>classpath:com/shr/dao/resources/mappers/*_mapper.xml</value> </list> </property>
接下去,進行配置雙數據源,通過properties配置文件進行切換,我們首先看下config.properties文件的內容:
# oracle configuration ora_driver=oracle.jdbc.driver.OracleDriver ora_url=jdbc:oracle:thin:@10.10.195.185:1521:sp5000 ora_username=shr ora_password=shr #mysql configuration mysql_driver=com.mysql.jdbc.Driver mysql_url=jdbc:mysql://10.10.193.111:3306/sp5000?useUnicode=true&characterEncoding=UTF-8 mysql_username=shr mysql_password=shr dataSource=mysql
可以看到分別對mysql以及oracle的數據庫進行了配置,並且標注了“dataSource”這個字段,這個就是進行數據源切換的配置。
接着來看一下修改后的applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.springframework.org/schema/aop classpath:/org/springframework/aop/config/spring-aop-3.0.xsd http://www.springframework.org/schema/context classpath:/org/springframework/context/config/spring-context-3.0.xsd http://www.springframework.org/schema/tx classpath:/org/springframework/transaction/config/spring-tx-3.0.xsd"> <!-- IoC配置 --> <!-- 掃描類包,將標注Spring注解的類自動轉化Bean,同時完成Bean的注入 --> <context:component-scan base-package="com.shr.dao" /> <context:component-scan base-package="com.shr.service" /> <!-- DAO配置 --> <context:property-placeholder location="classpath:config.properties"/> <bean id="mysql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${mysql_driver}"/> <property name="url" value="${mysql_url}"/> <property name="username" value="${mysql_username}"/> <property name="password" value="${mysql_password}"/> </bean> <bean id="oracle" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${ora_driver}"/> <property name="url" value="${ora_url}"/> <property name="username" value="${ora_username}"/> <property name="password" value="${ora_password}"/> </bean> <bean name="myBatisSQLInterceptor" class="com.shr.dao.MyBatisSQLInterceptor"></bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="${dataSource}" /> <property name="typeAliasesPackage" value="com.shr.dao.pojo,com.shr.dao.model" /> <property name="mapperLocations"> <list> <value>classpath:com/shr/dao/resources/${dataSource}mappers/*_mapper.xml</value> </list> </property> <!-- <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/> --> <property name="typeHandlersPackage" value="com.shr.dao" /> <property name="plugins"> <list> <ref bean="myBatisSQLInterceptor"/> </list> </property> </bean> <!-- 配置事務管理器 --> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="${dataSource}"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.shr.dao.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- <property name="markerInterface" value="com.shr.dao.mapper.ITemplateMapper"/> --> </bean> </beans>
可以看到這里配置了兩個數據源,分別為“mysql”和“oracle”,這個值對應config.properties配置文件中的dataSource字段。通過${dataSource}可以引用不同的數據源。
由於mysql和oracle之間sql語法有一定的差異性,所以這里需要采用兩種不同的mybatis配置文件:
<property name="mapperLocations"> <list> <value>classpath:com/shr/dao/resources/${dataSource}mappers/*_mapper.xml</value> </list> </property>
根據這個配置可以關聯兩種不同的mybatis配置文件,譬如mysql對應classpath:com/shr/dao/resources/mysqlmappers/*_mapper.xml。