<p>最近项目中用到多类型数据库,多库问题,然后有点懵了,自己试着做了个数据源切换的demo,不啰嗦了,上过程!</p><p>首先spring和mybaits整合的过程我就不多说了,先看applicationContext.xml:</p>
<?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:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean class="com.shangyon.ppa.utils.SpringUtil"></bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- 数据源 -->
<bean id="sqlServerDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.sqlserver.driverclass}"></property>
<property name="jdbcUrl" value="${jdbc.sqlserver.url}"></property>
<property name="user" value="${jdbc.sqlserver.username}"></property>
<property name="password" value="${jdbc.sqlserver.password}"></property>
<property name="initialPoolSize" value="3"></property>
<property name="minPoolSize" value="3"></property>
<property name="maxPoolSize" value="15"></property>
<property name="acquireIncrement" value="3"></property>
<property name="maxStatements" value="0"></property>
<property name="maxStatementsPerConnection" value="5"></property>
<property name="maxIdleTime" value="1800"></property>
</bean>
<bean id="mySqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.mysql.driverclass}"></property>
<property name="jdbcUrl" value="${jdbc.mysql.url}"></property>
<property name="user" value="${jdbc.mysql.username}"></property>
<property name="password" value="${jdbc.mysql.password}"></property>
<property name="initialPoolSize" value="3"></property>
<property name="minPoolSize" value="3"></property>
<property name="maxPoolSize" value="15"></property>
<property name="acquireIncrement" value="3"></property>
<property name="maxStatements" value="0"></property>
<property name="maxStatementsPerConnection" value="5"></property>
<property name="maxIdleTime" value="1800"></property>
</bean>
<span style="white-space:pre"> </span><!--切换数据源的核心-->
<bean id="multipleDataSource" class="com.shangyon.ppa.data.MultipleDataSource">
<property name="defaultTargetDataSource" ref="mySqlDataSource" />
<property name="targetDataSources">
<map>
<entry key="mySqlDataSource" value-ref="mySqlDataSource" />
<entry key="sqlServerDataSource" value-ref="sqlServerDataSource" />
</map>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="multipleDataSource" />
<property name="typeAliasesPackage" value="com.shangyon.ppa.bean.model">
</property>
<property name="mapperLocations"
value="classpath*:com/shangyon/ppa/bean/mapper/*Mapper.xml" />
</bean>
<!-- 事务管理器 -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="multipleDataSource"></property>
</bean>
<!-- 基于注解的事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<!-- 基于配置事务代理 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
<tx:method name="add*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="servicePointCut"
expression="execution(* com.shangyon.ppa.service.impl*.*(..))" />
<aop:advisor pointcut-ref="servicePointCut" advice-ref="transactionAdvice"
order="1" />
</aop:config>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="baseDao" abstract="true">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate" />
</bean>
<bean id="userDao" class="com.shangyon.ppa.dao.impl.UserDaoImpl"
parent="baseDao">
</bean>
<bean id="userService" class="com.shangyon.ppa.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
MultipleDataSource核心类
package com.shangyon.ppa.data;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
@Override
protected Object determineCurrentLookupKey() {
return dataSourceKey.get();
}
}
service测试切换数据源:
@Transactional(propagation=Propagation.REQUIRED)
public void testTransManage() {
User user = new User();
user.setName("111test");
user.setAge(221);
// 设置mysql数据源
MultipleDataSource.setDataSourceKey("mySqlDataSource");
userDao.addToMysql(user);
int a = 1/0;//测试事务
// 设置sqlserver数据源
MultipleDataSource.setDataSourceKey("sqlServerDataSource");
userDao.addToSqlserver(user);
}
jdbc.properties:
jdbc.sqlserver.url=jdbc:sqlserver://192.168.1.200:1433;DatabaseName=testdb
jdbc.sqlserver.username=sa
jdbc.sqlserver.password=sa
jdbc.sqlserver.driverclass=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.mysql.url=jdbc:mysql://localhost:3306/ppatest
jdbc.mysql.username=root
jdbc.mysql.password=root
jdbc.mysql.driverclass=com.mysql.jdbc.Driver
------------------------------------------------------------------------------
说了这么多,大神不屑看,和我一样的小白们又因为jar包和修改配置等折腾不好,直接上源码了,数据库什么的都要自己创,一些配置也要修改:
http://pan.baidu.com/s/1x0fZg