Spring集成
数据源的配置
C3P0连接池的配置
缓存的配置
AOP的XML配置和注解配置
IOC的XML配置和注解配置
Hibernate和IBatis以及MyBatis的配置差异
文件上传的配置
定时器的配置
国际化的配置
动态数据源切换的配置
思路:动态切换数据源确切的来说是在同一类型数据库的情况下的。意思就是说 , 在系统中的使用的数据库分布在多台数据库服务器或者在同台服务器上的多个数据库. 在运行时期间根据某种标识符来动态的选择当前操作的数据库.
1.数据源是相同类型的数据库: 一个SessionFactory+动态数据源+一个事务管理器
2.数据源是不同类型的数据库: 根据类型 配置多套SessionFactory
搭建过程:
1.切换数据源需要标识符,标识符是Object类型
lhp.example.context;
DBType {
dataSource1, dataSource2;
}
2.然后创建一个用于切换数据源(设置或者获得上下文)的工具类
lhp.example.context;
ContextHolder {
ThreadLocal<Object> holder = ThreadLocal<Object>();
setDbType(DBType dbType) {
holder.set(dbType);
}
DBType getDbType() {
(DBType) holder.get();
}
clearDbType() {
holder.remove();
}
}
3.创建动态数据源类,继承org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource这个类.
lhp.example.context;
java.util.logging.Logger;
org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
DynamicDataSource AbstractRoutingDataSource {
Logger logger = Logger.getLogger(DynamicDataSource..toString());
@Override
Object determineCurrentLookupKey() {
DBType key = ContextHolder.getDbType(); key;
}
}
4.然后配置多个数据源
<!-- 数据源1 : mysql -->
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/dec"/>
<property name="user" value="root"/>
<property name="password" value=""/>
</bean>
<!-- 数据源2 : mysql -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/lms"/>
<property name="user" value="root"/>
<property name="password" value=""/>
</bean>
<!-- 数据源3 : access -->
<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="jdbcUrl" value="jdbc:odbc:accessTest"/>
<property name="user" value="administrator"/>
<property name="password" value="XLZX0309"/>
</bean>
<!-- mysql 动态数据源设置-->
<bean id="mysqlDynamicDataSource" class="lhp.example.context.DynamicDataSource">
<property name="targetDataSources">
<!-- 标识符类型 -->
<map key-type="lhp.example.context.DBType">
<entry key="dataSource1" value-ref="dataSource1"/>
<entry key="dataSource2" value-ref="dataSource2"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource1"/>
</bean>
5.配置sessionFactory
org.hibernate.dialect.MySQLDialect
true
update
true 1, false 0
org.hibernate.dialect.SQLServerDialect
30
50
true
false
update
true 1, false 0
true
6.测试用例
package lhp.example.junit;
import org.junit.Assert.*;
import java.sql.DatabaseMetaData;
import lhp.example.context.ContextHolder;
import lhp.example.context.DBType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
ServiceTest {
ApplicationContext context;
String dataSource1_URL = ;
String dataSource2_URL = ;
String dataSource3_URL = ;
SessionFactory mysqlSessionFactory;
SessionFactory aceessSessionFactory;
@Before
setUp() throws Exception {
ContextHolder.setDbType(DBType.dataSource1);
String[] xmlFiles = String[] {
,
,
};
context = ClassPathXmlApplicationContext(xmlFiles);
mysqlSessionFactory = (SessionFactory) context.getBean();
aceessSessionFactory = (SessionFactory) context.getBean();
}
@SuppressWarnings()
@Test
mysqlDataSourceTest() {
{
Session mysqlSession = mysqlSessionFactory.openSession();
DatabaseMetaData meatData = mysqlSession.connection().getMetaData();
assertEquals(dataSource1_URL, meatData.getURL());
ContextHolder.setDbType(DBType.dataSource2);
mysqlSession = mysqlSessionFactory.openSession();
meatData = mysqlSession.connection().getMetaData();
assertEquals(dataSource2_URL, meatData.getURL());
} (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings()
@Test
accessDataSourceTest() {
{
Session accessSession = aceessSessionFactory.openSession();
DatabaseMetaData meatData = accessSession.connection().getMetaData();
assertEquals(dataSource3_URL, meatData.getURL());
} (Exception e) {
e.printStackTrace();
}
}
}