Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory(转)

1. Spring整合MyBatis切换SqlSessionFactory有两种方法,第一、 继承SqlSessionDaoSupport,重写获取SqlSessionFactory的方法。第二、继承SqlSessionTemplate 重写getSqlSessionFactory、getConfiguration和SqlSessionInterceptor这个拦截器。其中最为关键还是继承SqlSessionTemplate 并重写里面的方法。

    我们一般使用第二种方法,第二种方法有2 种配置:

单个sqlSessionFactory情况:需要在程序中(action 里面动态指定使用那个数据库连接池)

       spring-mybatis.xml配置如下:

     

  1. <!-- ========================================配置数据源========================================= -->  
  2.   
  3. <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 -->  
  4. <bean name="pepos" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  5.     <property name="url" value="${pepos.url}" />  
  6.     <property name="username" value="${pepos.username}" />  
  7.     <property name="password" value="${pepos.password}" />  
  8.       
  9.     <!-- 密码解密-->   
  10.     <property name="filters" value="config" />  
  11.     <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${pepos.publickey}" />  
  12.       
  13.     <!-- 初始化连接大小 -->  
  14.     <property name="initialSize" value="0" />  
  15.     <!-- 连接池最大使用连接数量 -->  
  16.     <property name="maxActive" value="20" />  
  17.     <!-- 连接池最大空闲 <property name="maxIdle" value="20" /> -->  
  18.     <!-- 连接池最小空闲 -->  
  19.     <property name="minIdle" value="0" />  
  20.     <!-- 获取连接最大等待时间 -->  
  21.     <property name="maxWait" value="60000" />  
  22.   
  23.     <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
  24.     <property name="poolPreparedStatements" value="true" />  
  25.     <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  26.   
  27.     <property name="validationQuery" value="SELECT 1" />  
  28.     <property name="testOnBorrow" value="false" />  
  29.     <property name="testOnReturn" value="false" />  
  30.     <property name="testWhileIdle" value="true" />  
  31.     <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  32.     <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  33.     <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  34.     <property name="minEvictableIdleTimeMillis" value="25200000" />  
  35.     <!-- 打开removeAbandoned功能 -->  
  36.     <property name="removeAbandoned" value="true" />  
  37.     <!-- 1800秒,也就是30分钟 -->  
  38.     <property name="removeAbandonedTimeout" value="1800" />  
  39.     <!-- 关闭abanded连接时输出错误日志 -->  
  40.     <property name="logAbandoned" value="true" />  
  41. </bean>  
  42.   
  43. <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 -->  
  44. <bean name="payment" class="com.alibaba.druid.pool.DruidDataSource"  
  45.     init-method="init" destroy-method="close">  
  46.     <property name="url" value="${payment.url}" />  
  47.     <property name="username" value="${payment.username}" />  
  48.     <property name="password" value="${payment.password}" />  
  49.       
  50.     <!-- 密码解密 -->  
  51.     <property name="filters" value="config" />  
  52.     <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${payment.publickey}" />  
  53.       
  54.     <!-- 初始化连接大小 -->  
  55.     <property name="initialSize" value="0" />  
  56.     <!-- 连接池最大使用连接数量 -->  
  57.     <property name="maxActive" value="20" />  
  58.     <!-- 连接池最大空闲 <property name="maxIdle" value="20" /> -->  
  59.     <!-- 连接池最小空闲 -->  
  60.     <property name="minIdle" value="0" />  
  61.     <!-- 获取连接最大等待时间 -->  
  62.     <property name="maxWait" value="60000" />  
  63.   
  64.     <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
  65.     <property name="poolPreparedStatements" value="true" />  
  66.     <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  67.   
  68.     <property name="validationQuery" value="SELECT 1" />  
  69.     <property name="testOnBorrow" value="false" />  
  70.     <property name="testOnReturn" value="false" />  
  71.     <property name="testWhileIdle" value="true" />  
  72.     <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  73.     <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  74.     <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  75.     <property name="minEvictableIdleTimeMillis" value="25200000" />  
  76.     <!-- 打开removeAbandoned功能 -->  
  77.     <property name="removeAbandoned" value="true" />  
  78.     <!-- 1800秒,也就是30分钟 -->  
  79.     <property name="removeAbandonedTimeout" value="1800" />  
  80.     <!-- 关闭abanded连接时输出错误日志 -->  
  81.     <property name="logAbandoned" value="true" />  
  82. </bean>  
  83.   
  84.        <!-- 需要基础spring的 AbstractRoutingDataSource 来实现动态设置数据库连接池  -->    
  85.     <bean id="dynamicDataSource" class="com.threeweidu.mallmanage.utils.DynamicDataSource">  
  86.     <property name="targetDataSources">  
  87.         <map key-type="java.lang.String">  
  88.             <entry value-ref="pepos" key="pepos"></entry>  
  89.             <entry value-ref="payment" key="payment"></entry>  
  90.                           


 

   多个sqlsessionFactroy 模式

   spring-mybatis.xml 配置文件

   

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-4.1.xsd   
  8.         http://www.springframework.org/schema/tx   
  9.         http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  10.         http://www.springframework.org/schema/aop   
  11.         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  12.         ">  
  13.   
  14.     <import resource="spring-mybatis-shop.xml" />  
  15.     <import resource="spring-mybatis-payment.xml" />  
  16.     <!--   
  17.     <import resource="spring-mybatis-pepos.xml" />  
  18.     <import resource="spring-mybatis-peposlog.xml" />  
  19.     <import resource="spring-mybatis-peposchat.xml" />  
  20.     <import resource="spring-mybatis-vaservice.xml" />  
  21.      -->  
  22.   
  23. </beans>  

spring-mybatis-payment.xml 配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-4.1.xsd   
  8.         http://www.springframework.org/schema/tx   
  9.         http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  10.         http://www.springframework.org/schema/aop   
  11.         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  12.         ">  
  13.       
  14.     <!-- 数据库连接池 -->  
  15.     <bean id="dataSource_payment" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  16.         <property name="url" value="${payment.url}" />  
  17.         <property name="username" value="${payment.username}" />  
  18.         <property name="password" value="${payment.password}" />  
  19.           
  20.         <!-- 密码解密 -->  
  21.         <property name="filters" value="config" />  
  22.         <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${payment.publickey}" />  
  23.           
  24.         <!-- 初始化连接大小 -->  
  25.         <property name="initialSize" value="3" />  
  26.         <!-- 连接池最大使用连接数量 -->  
  27.         <property name="maxActive" value="200" />  
  28.         <!-- 连接池最大空闲 <property name="maxIdle" value="20" /> -->  
  29.         <!-- 连接池最小空闲 -->  
  30.         <property name="minIdle" value="0" />  
  31.         <!-- 获取连接最大等待时间 -->  
  32.         <property name="maxWait" value="60000" />  
  33.   
  34.         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
  35.         <property name="poolPreparedStatements" value="true" />  
  36.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  37.   
  38.         <property name="validationQuery" value="SELECT 1" />  
  39.         <property name="testOnBorrow" value="false" />  
  40.         <property name="testOnReturn" value="false" />  
  41.         <property name="testWhileIdle" value="true" />  
  42.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  43.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  44.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  45.         <property name="minEvictableIdleTimeMillis" value="25200000" />  
  46.         <!-- 打开removeAbandoned功能 -->  
  47.         <property name="removeAbandoned" value="true" />  
  48.         <!-- 1800秒,也就是30分钟 -->  
  49.         <property name="removeAbandonedTimeout" value="1800" />  
  50.         <!-- 关闭abanded连接时输出错误日志 -->  
  51.         <property name="logAbandoned" value="true" />  
  52.     </bean>  
  53.       
  54.     <!-- MyBatis配置 -->  
  55.     <bean id="sqlSessionFactory_payment" class="org.mybatis.spring.SqlSessionFactoryBean">  
  56.         <property name="dataSource" ref="dataSource_payment" />  
  57.         <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->  
  58.         <property name="typeAliasesPackage" value="com.threeweidu.supplier.entity" />  
  59.         <!-- 显式指定Mapper文件位置 -->  
  60.         <property name="mapperLocations" value="classpath:/com/threeweidu/supplier/dao/mybatis/payment/mapper/*Mapper.xml" />  
  61.     </bean>  
  62.   
  63.     <!-- 配置扫描器 -->  
  64.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  65.         <!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 -->  
  66.         <property name="basePackage" value="com.threeweidu.supplier.dao.mybatis.payment" />  
  67.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_payment" />  
  68.     </bean>  
  69.   
  70.     <!-- 配置Spring的事务管理器 -->  
  71.     <bean id="transactionManager_payment" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  72.         <property name="dataSource" ref="dataSource_payment" />  
  73.     </bean>  
  74.       
  75.     <tx:annotation-driven transaction-manager="transactionManager_payment" proxy-target-class="true" />  
  76.       
  77.     <!-- 拦截器方式配置事务 -->  
  78.     <tx:advice id="transactionAdvice_payment" transaction-manager="transactionManager_payment">  
  79.         <tx:attributes>  
  80.             <tx:method name="get*"   propagation="REQUIRED" read-only="true"/>  
  81.             <tx:method name="find*"  propagation="REQUIRED" read-only="true"/>  
  82.             <tx:method name="load*"  propagation="REQUIRED" read-only="true"/>  
  83.             <tx:method name="init*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  84.             <tx:method name="save*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  85.             <tx:method name="add*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  86.             <tx:method name="register*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  87.             <tx:method name="update*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  88.             <tx:method name="delete*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  89.             <tx:method name="remove*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  90.             <tx:method name="*" propagation="SUPPORTS" />  
  91.         </tx:attributes>  
  92.     </tx:advice>  
  93.     <aop:config>  
  94.         <aop:advisor advice-ref="transactionAdvice_payment" pointcut="execution(* com.threeweidu.supplier.service..*Impl.*(..))" />  
  95.     </aop:config>  
  96.   
  97. </beans>  


 

参考:http://blog.csdn.net/xingxiupaioxue/article/details/51094091

原文地址:https://www.cnblogs.com/jpfss/p/8124979.html