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

 

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

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

 

 

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

       spring-mybatis.xml配置如下:

     

[java] view plain copy
print ?
  1.   
  2.   
  3.   
  4. "pepos" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  5.     "url" value="${pepos.url}" />  
  6.     "username" value="${pepos.username}" />  
  7.     "password" value="${pepos.password}" />  
  8.       
  9.        
  10.     "filters" value="config" />  
  11.     "connectionProperties" value="config.decrypt=true;config.decrypt.key=${pepos.publickey}" />  
  12.       
  13.       
  14.     "initialSize" value="0" />  
  15.       
  16.     "maxActive" value="20" />  
  17.       
  18.       
  19.     "minIdle" value="0" />  
  20.       
  21.     "maxWait" value="60000" />  
  22.   
  23.       
  24.     "poolPreparedStatements" value="true" />  
  25.     "maxPoolPreparedStatementPerConnectionSize" value="20" />  
  26.   
  27.     "validationQuery" value="SELECT 1" />  
  28.     "testOnBorrow" value="false" />  
  29.     "testOnReturn" value="false" />  
  30.     "testWhileIdle" value="true" />  
  31.       
  32.     "timeBetweenEvictionRunsMillis" value="60000" />  
  33.       
  34.     "minEvictableIdleTimeMillis" value="25200000" />  
  35.       
  36.     "removeAbandoned" value="true" />  
  37.       
  38.     "removeAbandonedTimeout" value="1800" />  
  39.       
  40.     "logAbandoned" value="true" />  
  41.   
  42.   
  43.   
  44. "payment" class="com.alibaba.druid.pool.DruidDataSource"  
  45.     init-method="init" destroy-method="close">  
  46.     "url" value="${payment.url}" />  
  47.     "username" value="${payment.username}" />  
  48.     "password" value="${payment.password}" />  
  49.       
  50.       
  51.     "filters" value="config" />  
  52.     "connectionProperties" value="config.decrypt=true;config.decrypt.key=${payment.publickey}" />  
  53.       
  54.       
  55.     "initialSize" value="0" />  
  56.       
  57.     "maxActive" value="20" />  
  58.       
  59.       
  60.     "minIdle" value="0" />  
  61.       
  62.     "maxWait" value="60000" />  
  63.   
  64.       
  65.     "poolPreparedStatements" value="true" />  
  66.     "maxPoolPreparedStatementPerConnectionSize" value="20" />  
  67.   
  68.     "validationQuery" value="SELECT 1" />  
  69.     "testOnBorrow" value="false" />  
  70.     "testOnReturn" value="false" />  
  71.     "testWhileIdle" value="true" />  
  72.       
  73.     "timeBetweenEvictionRunsMillis" value="60000" />  
  74.       
  75.     "minEvictableIdleTimeMillis" value="25200000" />  
  76.       
  77.     "removeAbandoned" value="true" />  
  78.       
  79.     "removeAbandonedTimeout" value="1800" />  
  80.       
  81.     "logAbandoned" value="true" />  
  82.   
  83.   
  84.            
  85.     "dynamicDataSource" class="com.threeweidu.mallmanage.utils.DynamicDataSource">  
  86.     "targetDataSources">  
  87.         "java.lang.String">  
  88.             "pepos" key="pepos">  
  89.             "payment" key="payment">  
  90.                           
 
    
    
    
        
        
        
        
         
        
        
        
        
        
        
        
        
        
        
        
        

        
        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        
        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
         
    
        
            
                
                
                            
[java] view plain copy
print ?
  1.                          
  2.       
  3.     "defaultTargetDataSource" ref="pepos">  
  4.       
  5.   
  6.   
  7.     "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  8.     "dataSource" ref="dynamicDataSource">  
  9.   
  10.   
  11.   
  12. "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  13.     "dataSource" ref="dynamicDataSource" />  
  14.       
  15.     "typeAliasesPackage" value="com.threeweidu.mallmanage.entity" />  
  16.       
  17.     "mapperLocations" value="classpath:/com/threeweidu/mallmanage/dao/mybatis/mapper/*Mapper.xml" />  
  18.   
  19.   
  20.   
  21. class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  22.       
  23.     "basePackage" value="com.threeweidu.mallmanage.dao.mybatis" />  
  24.     "sqlSessionFactoryBeanName" value="sqlSessionFactory" />  
  25.   
  26.   
  27.   
  28. "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  29.     "dataSource" ref="dynamicDataSource" />  
  30.   
  31.   
  32.     "transactionManager" proxy-target-class="true" />  
  33.   
  34.   
  35. "transactionAdvice" transaction-manager="transactionManager">  
  36.       
  37.         "get*"   propagation="REQUIRED" read-only="true"/>  
  38.         "find*"  propagation="REQUIRED" read-only="true"/>  
  39.         "load*"  propagation="REQUIRED" read-only="true"/>  
  40.         "init*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  41.         "save*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  42.         "add*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  43.         "register*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  44.         "update*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  45.         "delete*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  46.         "remove*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  47.             "*" propagation="SUPPORTS" />  
  48.       
  49.   
  50.   
  51.     "transactionPointcut" expression="execution(* com.threeweidu.mallmanage.service..*Impl.*(..))"/>  
  52.     "transactionPointcut" advice-ref="transactionAdvice"/>  
  53.      
  54.      
  55. lt;/beans>  
                        
        
        
        
    
 
    
        
    

    
    
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
    

    

    
    
        
            
            
            
            
            
            
            
            
            
            
            
        
    
    
        
        
    
    


 

   多个sqlsessionFactroy 模式

   spring-mybatis.xml 配置文件

   

[java] view plain copy
print ?
  1. "1.0" encoding="UTF-8"?>  
  2. "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.   
  18.   



    
    
    

 

spring-mybatis-payment.xml 配置

[java] view plain copy
print ?
  1. "1.0" encoding="UTF-8"?>  
  2. "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.     "dataSource_payment" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  16.         "url" value="${payment.url}" />  
  17.         "username" value="${payment.username}" />  
  18.         "password" value="${payment.password}" />  
  19.           
  20.           
  21.         "filters" value="config" />  
  22.         "connectionProperties" value="config.decrypt=true;config.decrypt.key=${payment.publickey}" />  
  23.           
  24.           
  25.         "initialSize" value="3" />  
  26.           
  27.         "maxActive" value="200" />  
  28.           
  29.           
  30.         "minIdle" value="0" />  
  31.           
  32.         "maxWait" value="60000" />  
  33.   
  34.           
  35.         "poolPreparedStatements" value="true" />  
  36.         "maxPoolPreparedStatementPerConnectionSize" value="20" />  
  37.   
  38.         "validationQuery" value="SELECT 1" />  
  39.         "testOnBorrow" value="false" />  
  40.         "testOnReturn" value="false" />  
  41.         "testWhileIdle" value="true" />  
  42.           
  43.         "timeBetweenEvictionRunsMillis" value="60000" />  
  44.           
  45.         "minEvictableIdleTimeMillis" value="25200000" />  
  46.           
  47.         "removeAbandoned" value="true" />  
  48.           
  49.         "removeAbandonedTimeout" value="1800" />  
  50.           
  51.         "logAbandoned" value="true" />  
  52.       
  53.       
  54.       
  55.     "sqlSessionFactory_payment" class="org.mybatis.spring.SqlSessionFactoryBean">  
  56.         "dataSource" ref="dataSource_payment" />  
  57.           
  58.         "typeAliasesPackage" value="com.threeweidu.supplier.entity" />  
  59.           
  60.         "mapperLocations" value="classpath:/com/threeweidu/supplier/dao/mybatis/payment/mapper/*Mapper.xml" />  
  61.       
  62.   
  63.       
  64.     class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  65.           
  66.         "basePackage" value="com.threeweidu.supplier.dao.mybatis.payment" />  
  67.         "sqlSessionFactoryBeanName" value="sqlSessionFactory_payment" />  
  68.       
  69.   
  70.       
  71.     "transactionManager_payment" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  72.         "dataSource" ref="dataSource_payment" />  
  73.       
  74.       
  75.     "transactionManager_payment" proxy-target-class="true" />  
  76.       
  77.       
  78.     "transactionAdvice_payment" transaction-manager="transactionManager_payment">  
  79.           
  80.             "get*"   propagation="REQUIRED" read-only="true"/>  
  81.             "find*"  propagation="REQUIRED" read-only="true"/>  
  82.             "load*"  propagation="REQUIRED" read-only="true"/>  
  83.             "init*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  84.             "save*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  85.             "add*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  86.             "register*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  87.             "update*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  88.             "delete*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  89.             "remove*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>  
  90.             "*" propagation="SUPPORTS" />  
  91.           
  92.       
  93.       
  94.         "transactionAdvice_payment" pointcut="execution(* com.threeweidu.supplier.service..*Impl.*(..))" />  
  95.       
  96.   
  97.   


    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        
        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
    
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
    
    
    
    
    
    
        
            
            
            
            
            
            
            
            
            
            
            
        
    
    
        
    


 

 

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

 

 

你可能感兴趣的:(Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory(转))