你的位置:首页 > 软件开发 > Java > Spring+spring mvc+mybatis 多数据源切换

Spring+spring mvc+mybatis 多数据源切换

发布时间:2015-08-10 01:00:05
spring mvc+mybatis+多数据源切换 选取oracle,mysql作为例子切换数据源。oracle为默认数据源,在测试的action中,进行mysql和oracle的动态切换。 整理的比较粗略,将代码分享给大家,【源码地址获取】web.Java代码 ...

spring mvc+mybatis+多数据源切换 选取oracle,mysql作为例子切换数据源。oracle为默认数据源,在测试的action中,进行mysql和oracle的动态切换。 dispatcher.Java代码  Spring+spring mvc+mybatis 多数据源切换

  1. <mvc:annotation-driven />  
  2.     <context:component-scan base-package="com.trac" />  
  3.   
  4.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />  
  5.   
  6.     <!-- freemarker config -->  
  7.     <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  8.         <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />  
  9.         <property name="freemarkerVariables">  
  10.             <map>  
  11.                 <entry key=""fm
  12.             </map>  
  13.         </property>  
  14.         <property name="freemarkerSettings">  
  15.             <props>  
  16.                 <prop key="defaultEncoding">UTF-8</prop>  
  17.             </props>  
  18.         </property>  
  19.     </bean>  
  20.   
  21.     <bean id="fmclass="freemarker.template.utility.
  22.   
  23.     <!-- View resolvers can also be configured with ResourceBundles or 
  24.         If you need different view resolving based on Locale, you have to use the   
  25.         resource bundle resolver. -->  
  26.     <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  27.         <property name="exposeRequestAttributes" value="true" />  
  28.         <property name="exposeSessionAttributes" value="true" />  
  29.         <property name="exposeSpringMacroHelpers" value="true" />  
  30.         <property name="contentType" value="text/html;charset=UTF-8" />  
  31.         <property name="cache" value="true" />  
  32.         <property name="prefix" value="" />  
  33.         <property name="suffix" value=".ftl" />  
  34.     </bean>  
applicationContext.Java代码  Spring+spring mvc+mybatis 多数据源切换
  1. <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  2.     </bean>  
  3.       
  4.     <bean id="mySqlDataSource" parent="parentDataSource">  
  5.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  6.         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
  7.         <property name="username" value="root"></property>  
  8.         <property name="password" value="root"></property>  
  9.     </bean>  
  10.       
  11.     <bean id="oracleDataSource" parent="parentDataSource">  
  12.         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
  13.         <property name="url" value="jdbc:oracle:thin:@10.16.17.40:1531:addb"></property>  
  14.         <property name="username" value="trac"></property>  
  15.         <property name="password" value="trac"></property>  
  16.     </bean>  
  17.       
  18.     <bean id="dataSource" class="com.trac.dao.datasource.DataSources">  
  19.         <property name="targetDataSources">  
  20.             <map key-type="java.lang.String">  
  21.                 <entry value-ref="mySqlDataSource" key="MYSQL"></entry>  
  22.                 <entry value-ref="oracleDataSource" key="ORACLE"></entry>  
  23.             </map>  
  24.         </property>  
  25.         <property name="defaultTargetDataSource" ref="oracleDataSource"></property>  
  26.     </bean>  
  27.   
  28.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  29.         <property name="dataSource" ref="dataSource" />  
  30.     </bean>  
  31.   
  32.     <!-- 创建SqlSessionFactory,同时指定数据源和mapper -->  
  33.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  34.         <property name="dataSource" ref="dataSource" />  
  35.         <property name="mapperLocations" value="classpath*:com/trac/ibatis/dbcp/*.
  36.     </bean>  
  37.   
  38.     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
  39.         <constructor-arg index="0" ref="sqlSessionFactory" />  
  40.     </bean>  
  41.   
  42.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  43.         <property name="basePackage" value="com.trac.dao" />  
  44.     </bean>  
配置 parentDataSource 的父bean.再配置多个数据源继承这个父bean,对driverClass,url,username,password,等数据源连接参数进行各自的重写。例如 mySqlDataSource ,在 DataSources bean中注入所有要切换的数据源,并且设置默认的数据源。 DataSourceInstances.java 

Java代码  Spring+spring mvc+mybatis 多数据源切换
  1. public class DataSourceInstances{  
  2.     public static final String MYSQL="MYSQL";  
  3.     public static final String ORACLE="ORACLE";  
  4. }     
  5.   
  6. 定义数据源的标识, 和applicationContext.

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Spring+spring mvc+mybatis 多数据源切换

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录