Spring配置文件


 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();
        }
    }
}
 

转载于:https://my.oschina.net/yunsy/blog/297720

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值