多数据源只不过在dao层配置多配置一个数据库驱动连接信息,添加一个切换数据库对象,设置一个主要默认数据库,须要其余数据库须要操做室利用公共数据库切换类实现数据库切换。java
###2、配置mysql
1.基本web.xml配置不变和普通配置springmvc同样web
2.spring上下文配置也不变springMVC-context.xmlspring
3.jdbc.properties双数据库源sql
driver=com.mysql.jdbc.Driver url=jdbc\:mysql\://127.0.0.1\:3306/datebasename?useUnicode\=true&characterEncoding\=utf-8 username=root password=root driver2=oracle.jdbc.driver.OracleDriver url2=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:XE username2=ROOT password2=ROOT
4.spring-dao.xml数据库链接 注意每个数据库配置id,后面数据库切换工具类要使用数据库
<?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:jdbc="http://www.springframework.org/schema/jdbc" 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.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 数据源配置1 --> <bean id="mysqlData" 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}" /> <property name="initialSize" value="5" /> <!-- 初始链接数量 --> <property name="maxActive" value="30" /> <!-- 最大链接数量 --> <property name="maxIdle" value="5" /> <!-- 空闲链接数量 --> <property name="maxWait" value="60000" /> <!-- 一个查询1分钟内没有返回,自动放弃 --> <property name="validationQuery" value="SELECT 1" /> <!-- 数据库链接可用性测试语句 --> <property name="testOnBorrow" value="true" /> <!-- 每次获取一个链接的时候,验证一下链接是否可用,语句在validationQuery里面 --> <property name="removeAbandoned" value="true" /> <!-- 自动处理链接未关闭的问题,Setting this to true can recover db connections from poorly written applications which fail to close a connection. --> <property name="removeAbandonedTimeout" value="300" /> <!-- 链接使用后5分钟未关闭,则抛弃 --> </bean> <!-- 数据源配置 2 --> <bean id="oracleData" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver2}" /> <property name="url" value="${url2}" /> <property name="username" value="${username2}" /> <property name="password" value="${password2}" /> <property name="initialSize" value="5" /> <!-- 初始链接数量 --> <property name="maxActive" value="30" /> <!-- 最大链接数量 --> <property name="maxIdle" value="5" /> <!-- 空闲链接数量 --> <property name="maxWait" value="60000" /> <!-- 一个查询1分钟内没有返回,自动放弃 --> <property name="validationQuery" value="select 1 from dual" /> <!-- oracle数据库链接可用性测试语句 --> <property name="testOnBorrow" value="true" /> <!-- 每次获取一个链接的时候,验证一下链接是否可用,语句在validationQuery里面 --> <property name="removeAbandoned" value="true" /> <!-- 自动处理链接未关闭的问题,Setting this to true can recover db connections from poorly written applications which fail to close a connection. --> <property name="removeAbandonedTimeout" value="300" /> <!-- 链接使用后5分钟未关闭,则抛弃 --> </bean> <!-- 动态配置数据源 --> <bean id="dataSource" class="com.xxxx.dataSource.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="mysqlData" key="mysqlData"></entry> <entry value-ref="oracleData" key="oracleData"></entry> </map> </property> <property name="defaultTargetDataSource" ref="mysqlData"></property> <!-- 默认使用mysql的数据源 --> </bean> <!-- MyBatis配置 建立SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.xxxx.entity" /> </bean> <!--dao层接口和mybatis映射xml在同一目录下 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xxxx.dao" /> </bean> </beans>
5.spring-service.xml 事务配置(已经注释掉)express
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.wanliyun.service" /> <!-- 事务控制 对MyBatis操做数据库 spring使用JDBC事务控制类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 实现基于注解的事务管理 --> <!-- <tx:annotation-driven transaction-manager="txManager" /> --> <!-- 配置事务传播特性 --> <!-- <tx:advice id="TestAdvice" transaction-manager="transactionManager"> --> <!-- <tx:attributes> --> <!-- <tx:method name="save*" propagation="REQUIRED"/> --> <!-- <tx:method name="del*" propagation="REQUIRED"/> --> <!-- <tx:method name="update*" propagation="REQUIRED"/> --> <!-- <tx:method name="add*" propagation="REQUIRED"/> --> <!-- <tx:method name="modify*" propagation="REQUIRED"/> --> <!-- <tx:method name="find*" propagation="REQUIRED"/> --> <!-- <tx:method name="get*" propagation="REQUIRED"/> --> <!-- <tx:method name="apply*" propagation="REQUIRED"/> --> <!-- <tx:method name="*" propagation="REQUIRED"/> --> <!-- </tx:attributes> --> <!-- </tx:advice> --> <!-- 配置参与事务的类 <aop:advisor pointcut="execution(* com.zhcv.vote.*.service.*.*(..)) advice-ref="TestAdvice"/> --> <!-- <aop:config> --> <!-- <aop:pointcut id="allTestServiceMethod" expression="execution(* com.wanliyun.service.*.*(..))"/> --> <!-- <aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" /> --> <!-- </aop:config> --> </beans>
6.三个java工具类apache
/** * */ package com.wanliyun.dataSource; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * 写一个DynamicDataSource类来继承AbstractRoutingDataSource, * 并重写determineCurrentLookupKey()方法,来达到动态切换数据库 * * @author lennon * @time 2017-4-26 下午3:17:59 * @version */ public class DynamicDataSource extends AbstractRoutingDataSource { /* * (non-Javadoc) * * @see * org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource# * determineCurrentLookupKey() */ @Override protected Object determineCurrentLookupKey() { // TODO Auto-generated method stub return DatabaseContextHolder.getDbType(); } }
注意下面这工具中的数据库要与dao配置中的数据源id相同mybatis
/** * */ package com.wanliyun.dataSource; /** * [@author](https://my.oschina.net/arthor) lennon 数据源配置 * [@time](https://my.oschina.net/u/126678) 2017-4-26 下午5:11:13 * [@version](https://my.oschina.net/u/931210) */ public class DataSourceType { public static String MysqlType = "mysqlData"; public static String OracleType = "oracleData"; static { } }
/** * */ package com.wanliyun.dataSource; /** * [@author](https://my.oschina.net/arthor) lennon 切换数据源类 * * [@time](https://my.oschina.net/u/126678) 2017-4-26 下午3:16:17 * @version */ public class DatabaseContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); // 设置要使用的数据源 public static void setDbType(String dbType) { contextHolder.set(dbType); } // 获取数据源 public static String getDbType() { return contextHolder.get(); } // 清除数据源,使用默认的数据源 public static void clearDbType() { contextHolder.remove(); } /** * @author lennon * * @param args * @time 2017-4-26 下午3:16:17 * @version */ public static void main(String[] args) { // TODO Auto-generated method stub } }
7.其余spring项目的配置注解使用 都不变 该怎么用还怎么用 只要注意切换数据库mvc
/** * controller */ package com.wanliyun.controller; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.wanliyun.dataSource.DataSourceType; import com.wanliyun.dataSource.DatabaseContextHolder; import com.wanliyun.service.UserService; /** * @author lennon * * @time 2017-4-26 下午5:19:36 * @version */ @Controller public class UserController { @Resource UserService UserService; @RequestMapping(value = "user") public void saveUser(HttpServletRequest request, HttpServletResponse response) { //切换数据源 DatabaseContextHolder.setDbType(DataSourceType.OracleType); UserService.save(); } }
/** * service */ package com.wanliyun.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.wanliyun.dao.UserMapper; import com.wanliyun.entity.User; import com.wanliyun.mongoDao.mongoDao; /** * @author lennon * * @time 2017-4-26 下午5:19:58 * @version */ @Service public class UserService { @Autowired UserMapper UserMapper; public void save() { User user = new User(); user.setUserName("chen"); mongoDao mongoDao = new mongoDao(); mongoDao.insert(user); } }
就是这样没什么特别之处,记录一下方便之后方便查看