匆匆忙忙整理了 这篇文章http://blog.csdn.net/rainyspring4540/article/details/51828573
回头看这篇文章的代码来学习原理还是够呛的,由于涉及建库后生成的新数据源需要动态加入原有数据源列表中,使得代码里要通过重新构造AbstractRoutingDataSource类,即通过extends AbstractDataSource抽象类,implements InitializingBean接口,实现增强型的MyAbstractRoutingDataSource,来达到动态数据源的新增和切换并存,整个过程有点复杂,不便于自己入门,呵呵。。。
不过直接下载上篇文章里现成的代码,还是可以快速搭建起包含一整套分库逻辑(备份主库、建子库、为子库动态创建数据源、使用和切换数据源),所以上篇文章就作为快速搭建的案例吧。
下面的文章主要是上篇文章的简化版本,目的在于仅仅抽离出单纯的切换多数据源,便于更好的理解通过单纯的继承AbstractRoutingDataSource来自定义真正的动态数据源DynamicDataSource类,简单好理解,入门更快。
这篇文章主要讲比较常见的《固定N数据源,切换多数据源》
代码地址:http://download.csdn.net/detail/rainyspring4540/9569066
代码很简单,代码包中未包含库文件,我简单说下:
环境:mysql5.6 ,win7,eclipse kepler ,jQuery v13 ,springmvc4+hibernate4
根据配置文件:你需要创建库名为db,a,b的三个库,再分别创建表create table msg(name varchar(255) primary key, note varchar(255));就ok了。
关注2个文件:
package util.db;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static String getCurrentLookupKey() {
return (String) contextHolder.get();
}
public static void setCurrentLookupKey(String currentLookupKey) {
contextHolder.set(currentLookupKey);
}
@Override
public Object determineCurrentLookupKey() {
return getCurrentLookupKey();
}
}
和配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context
http://www.springframework.org/schema/context/spring-context-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="controller"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 单独文件 -->
<!-- <property name="location" value="/WEB-INF/config.properties" /> -->
<!-- 多个文件 -->
<property name="locations">
<list>
<!-- method1 标准配置 -->
<value>classpath*:/config/config.properties</value>
<!-- method2 -->
<!-- <value>/WEB-INF/config.properties</value> -->
<!--method3 服务器生产环境配置 -->
<!-- <value>file:/var/myapp/application.server.properties</value> -->
</list>
</property>
</bean>
<!-- 连接mysql -->
<bean id="defaultDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${main.driver}" />
<property name="url" value="${main.url}" />
<property name="username" value="${main.username}" />
<property name="password" value="${main.password}" />
</bean>
<bean id="ds_a" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${a.driver}" />
<property name="url" value="${a.url}" />
<property name="username" value="${a.username}" />
<property name="password" value="${a.password}" />
</bean>
<bean id="ds_b" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${b.driver}" />
<property name="url" value="${b.url}" />
<property name="username" value="${b.username}" />
<property name="password" value="${b.password}" />
</bean>
<bean id="dynamicDataSource" class="util.db.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="defaultDataSource" value-ref="defaultDataSource" />
<entry key="ds_a" value-ref="ds_a" />
<entry key="ds_b" value-ref="ds_b" />
</map>
</property>
<property name="defaultTargetDataSource" ref="defaultDataSource" />
</bean>
<!-- 类型2:DB2、SQL Server、MySQL 等非 Oracle 的其它数据库,则只要简单配置一个 DefaultLobHandler -->
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<!--Hibernate4 sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="packagesToScan">
<list>
<!-- 可以加多个包 -->
<value>entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto" >${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务异常封装 -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<!-- jdbc事务管理器 -->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource_mysql" />
</property>
</bean> -->
<!-- 启用注解 -->
<context:annotation-config />
<!-- 注解事务驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- ViewResolver -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="suffix" value=".jsp" />
<!-- 设置解析器的优先级 InternalResourceViewResolver必须总是赋予最低的优先级(最大的order值), 因为不管返回什么视图名称,它都将解析视图。如果它的优先级高于其它解析器的优先级的话,
它将使得其它具有较低优先级的解析器没有机会解析视图 -->
<property name="order" value="0" />
</bean>
</beans>