Springmvc+mybatis多数据源切换

源码地址获取:http://blog.sina.com.cn/s/blog_150df94af0102vngn.html

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

web.xml 

Java代码  收藏代码

  1. <context-param>  

  2.         <param-name>webAppRootKey</param-name>  

  3.         <param-value>trac</param-value>  

  4.     </context-param>  

  5.   

  6.     <!-- Spring的log4j监听器 -->  

  7.     <listener>  

  8.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  

  9.     </listener>  

  10.   

  11.     <!-- 字符集 过滤器 -->  

  12.     <filter>  

  13.         <filter-name>CharacterEncodingFilter</filter-name>  

  14.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  

  15.         <init-param>  

  16.             <param-name>encoding</param-name>  

  17.             <param-value>utf8</param-value>  

  18.         </init-param>  

  19.         <init-param>  

  20.             <param-name>forceEncoding</param-name>  

  21.             <param-value>true</param-value>  

  22.         </init-param>  

  23.     </filter>  

  24.     <filter-mapping>  

  25.         <filter-name>CharacterEncodingFilter</filter-name>  

  26.         <url-pattern>/*</url-pattern>  

  27.     </filter-mapping>  

  28.   

  29.     <!-- Spring view分发器 -->  

  30.     <servlet>  

  31.         <servlet-name>dispatcher</servlet-name>  

  32.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  

  33.         <init-param>  

  34.             <param-name>contextConfigLocation</param-name>  

  35.             <param-value>/WEB-INF/dispatcher.xml</param-value>  

  36.         </init-param>  

  37.         <load-on-startup>1</load-on-startup>  

  38.     </servlet>  

  39.     <servlet-mapping>  

  40.         <servlet-name>dispatcher</servlet-name>  

  41.         <url-pattern>*.action</url-pattern>  

  42.     </servlet-mapping>  

  43.       

  44.     <listener>  

  45.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

  46.     </listener>  



dispatcher.xml 

Java代码  收藏代码

  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="xml_escape" value-ref="fmXmlEscape" />  

  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="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />  

  22.   

  23.     <!-- View resolvers can also be configured with ResourceBundles or XML files.   

  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.xml 

Java代码  收藏代码

  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/*.xml" />  

  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代码  收藏代码

  1. public class DataSourceInstances{  

  2.     public static final String MYSQL="MYSQL";  

  3.     public static final String ORACLE="ORACLE";  

  4. }     

  5.   

  6. 定义数据源的标识, 和applicationContext.xml中 DataSources 的 targetDataSources 的key对应  




DataSourceSwitch.java 

Java代码  收藏代码

  1. public class DataSourceSwitch{  

  2.     private static final ThreadLocal contextHolder=new ThreadLocal();  

  3.       

  4.     public static void setDataSourceType(String dataSourceType){  

  5.         contextHolder.set(dataSourceType);  

  6.     }  

  7.       

  8.     public static String getDataSourceType(){  

  9.         return (String) contextHolder.get();  

  10.     }  

  11.       

  12.     public static void clearDataSourceType(){  

  13.         contextHolder.remove();  

  14.     }  

  15. }  




DataSources.java 

Java代码  收藏代码

  1. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  

  2.   

  3. public class DataSources extends AbstractRoutingDataSource{  

  4.   

  5.     @Override  

  6.     protected Object determineCurrentLookupKey() {  

  7.         return DataSourceSwitch.getDataSourceType();  

  8.     }  

  9.   

  10. }  

  11.   

  12. 配置于applicationContext 中,线程局部变量 ThreadLocal contextHolder 保存当前需要的数据源类型, 当 DataSourceSwitch.setDataSourceType(DataSourceInstances.XXX) 保存当前需要的数据源 类型的时候,DataSources 会从当前线程中查找线程变量的数据源类型,从而决定使用何种数据源  




TestAction.java 

Java代码  收藏代码

  1. @Controller   

  2. @SuppressWarnings("unused")  

  3. public class TestAction {  

  4. @Autowired  

  5.     TestMapper testMapper;  

  6.   

  7.    @RequestMapping("/test.action")  

  8.     public ModelAndView test(  

  9.             HttpServletRequest request,  

  10.             HttpServletResponse resp){  

  11.         ModelAndView model = new ModelAndView("test");  

  12.         model.addObject("test1""这是一个测试,获取默认数据连接MYSQL:"+testMapper.test());  

  13.         DataSourceSwitch.setDataSourceType(DataSourceInstances.ORACLE);  

  14.         model.addObject("test2""这是一个测试,获取数据连接ORACLE:"+testMapper.test());  

  15.         DataSourceSwitch.setDataSourceType(DataSourceInstances.MYSQL);  

  16.         model.addObject("test3""这是一个测试,获取数据连接MYSQL:"+testMapper.test());  

  17.         return model;  

  18.     }  


你可能感兴趣的:(springMVC,shiro,mybatis,bootstrap,Restful)