• spring+mybatis多数据源切换


    1.applicationContext.xml配置

    <?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:task="http://www.springframework.org/schema/task" 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/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <!--配置mysql数据库参数 -->
    <context:property-placeholder location="mysql.properties" />

    <!--多个数据源配置 -->
    <bean id="db1" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <!-- 连接数据库参数 -->
    <property name="driverClassName" value="${db1.driver}" />
    <property name="url" value="${db1.url}" />
    <property name="username" value="${db1.username}" />
    <property name="password" value="${db1.password}" />

    <!--连接池参数 -->
    <property name="initialSize" value="10" />
    <property name="maxActive" value="500" />
    <property name="maxIdle" value="40" />
    <property name="minIdle" value="10" />
    </bean>

    <bean id="db2" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <!-- 连接数据库参数 -->
    <property name="driverClassName" value="${db2.driver}" />
    <property name="url" value="${db2.url}" />
    <property name="username" value="${db2.username}" />
    <property name="password" value="${db2.password}" />

    <!--连接池参数 -->
    <property name="initialSize" value="10" />
    <property name="maxActive" value="500" />
    <property name="maxIdle" value="40" />
    <property name="minIdle" value="10" />
    </bean>

    <bean id="dataSource" class="com.ckd.datasource.mybatis.DynamicDataSource">
    <property name="targetDataSources">
    <map key-type="java.lang.String">
    <entry key="db1" value-ref="db1" />
    <entry key="db2" value-ref="db2" />
    </map>
    </property>
    <property name="defaultTargetDataSource" ref="db2" />
    </bean>

    <!-- mybatis配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
    </bean>
    <!--获取sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0">
    <ref bean="sqlSessionFactory"/>
    </constructor-arg>
    </bean>
    <!-- 事务管理器配置,单数据源事务 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>

    </beans>

    2.具体实现切换的操作类:

    /**
    * @fileName DynamicDataSource.java
    * @author chenkaideng
    * @date 2015年8月27日
    * @describe 动态获取数据源
    */
    public class DynamicDataSource extends AbstractRoutingDataSource{
      @Override
      protected Object determineCurrentLookupKey() {
        // TODO Auto-generated method stub
        return DataSourceContextHolder.getDbType();
      }
    }

    /**
    * @fileName DataSourceContextHolder.java
    * @author chenkaideng
    * @date 2015年8月27日
    * @describe 数据源设值Holder类
    */
    public class DataSourceContextHolder {
      private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

      public static void setDbType(String dbType) {
        contextHolder.set(dbType);
      }

      public static String getDbType() {
        return ((String) contextHolder.get());
      }

      public static void clearDbType() {
        contextHolder.remove();
      }
    }

     -》先是加载applicationContext.xml文件applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        -》然后设置数据源DataSourceContextHolder.setDbType("db1");
        -》接着从applicationContext 中获取sqlSession = (SqlSession) applicationContext.getBean("sqlSession");
        -》最后就可以拿这个sqlSession去做增删改查的操作
     
    注意:不用对这个sqlSession做close和comit的操作,因为都已经由spring自己管理了,不用手动做这些操作。
  • 相关阅读:
    PHP各种读取文件的函数效率对比
    一篇不错的session与cookie机制的文章
    Yahoo!团队实践分享:网站性能优化的34条黄金守则
    PHP魔术函数集锦
    MySQL索引类型一览 让MySQL高效运行起来
    Mysql的索引和查询优化
    MySQL简单查询性能分析
    kafka进阶一
    Event Loop
    如何提高 Webpack 的构建速度、优化前端性能?NPM 的模块构建优先选用ES6还是ES5?如何吃透复杂的插件体系,源码如何阅读?
  • 原文地址:https://www.cnblogs.com/heqq-bky/p/4905157.html
Copyright © 2020-2023  润新知