实战中的多数据源的配置问题

在实际项目中,有许多 项目要用到不同的数据 库或者 不同类型的数据 源,对于 这些,一般是要在相应的文件中配置,如下面一样,将所的有dao及mapper分不同的文件夹存放 ,,

<?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:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:tx="http://www.springframework.org/schema/tx"  
   xmlns:aop="http://www.springframework.org/schema/aop"  
   xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop  
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/tx 
                  http://www.springframework.org/schema/tx/spring-tx.xsd">
   <!-- 自动扫描 -->
   <context:component-scan base-package="com.cn.tianxia" />
   <!-- 引入配置文件 -->
   <bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="classpath:jdbc.properties" />
   </bean>  
   
    <!-- base dataSource -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
       <property name="url">
         <value>${url}</value>
      </property>
      <property name="username">
         <value>${username}</value>
      </property>
      <property name="password">
         <value>${password}</value>
      </property>
      <property name="initialSize">
         <value>0</value>
      </property>
      <property name="minIdle">
         <value>0</value>
      </property>
      <property name="maxActive">
         <value>100</value>
      </property> 
      
      <!-- 自动清除无用连接 --> 
      <property name="removeAbandoned"> 
         <value>true</value> 
      </property> 
      <!-- 清除无用连接的等待时间 --> 
      <property name="removeAbandonedTimeout"> 
         <value>3600</value> 
      </property>
      <property name="timeBetweenEvictionRunsMillis" value="600000" />

      <property name="minEvictableIdleTimeMillis" value="3000000" />
      
      <property name="validationQuery" value="SELECT 'x'" />
      <property name="testOnBorrow" value="true" />
      
      <property name="testOnReturn" value="true" />
      <property name="testWhileIdle" value="true" /> 
      <property name="proxyFilters">
         <list>
            <ref bean="stat-filter" />
            <ref bean="wall-filter" />
         </list>
      </property>
    </bean> 

   <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:com/cn/tianxia/mapper/**/*.xml"/>  
      <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
      <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model" 
         /> -->
   </bean>

   <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.cn.tianxia.dao" />
      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
      
   </bean> 
   
   

   
    <!-- base dataSource -->
    <bean id="dataSourceData" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
       <property name="url">
         <value>${dataurl}</value>
      </property>
      <property name="username">
         <value>${datausername}</value>
      </property>
      <property name="password">
         <value>${datapassword}</value>
      </property>
      <property name="initialSize">
         <value>0</value>
      </property>
      <property name="minIdle">
         <value>0</value>
      </property>
      <property name="maxActive">
         <value>100</value>
      </property>
      
      <property name="poolPreparedStatements">
         <value>true</value>
      </property>
      
      <property name="maxOpenPreparedStatements">
         <value>100</value>
      </property>
      
      <!-- 自动清除无用连接 --> 
      <property name="removeAbandoned"> 
         <value>true</value> 
      </property> 
      <!-- 清除无用连接的等待时间 --> 
      <property name="removeAbandonedTimeout"> 
         <value>3600</value> 
      </property>
      <property name="timeBetweenEvictionRunsMillis" value="600000" />

      <property name="minEvictableIdleTimeMillis" value="3000000" />
      
      <property name="validationQuery" value="SELECT 'x'" />
      <property name="testOnBorrow" value="true" />
      
      <property name="testOnReturn" value="true" />
      <property name="testWhileIdle" value="true" /> 
      <property name="proxyFilters">
         <list>
            <ref bean="stat-filter" />
            <ref bean="wall-filter" />
         </list>
      </property>
    </bean> 
    
    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
      <property name="slowSqlMillis" value="1000" />
      <property name="logSlowSql" value="true" />
      <property name="mergeSql" value="true" />
   </bean>

   <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
      <property name="dbType" value="mysql" />
      <property name="config" ref="wall-filter-config" />
   </bean>
   
   <bean id="wall-filter-config" class="com.alibaba.druid.wall.WallConfig">
      <property name="multiStatementAllow" value="true" />
   </bean>

   <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
   <bean id="sqlSessionFactoryData" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSourceData" />
        <property name="mapperLocations" value="classpath*:com/cn/tianxia/datamapper/**/*.xml"/>  
      <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> 
   </bean>

   <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.cn.tianxia.datadao" />
      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryData"></property>
   </bean>  
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值