spring mvc+mybatis+多数据源切换 选取oracle,mysql作为例子切换数据源。oracle为默认数据源,在测试的action中,进行mysql和oracle的动态切换。 整理的比较粗略,将代码分享给大家,【源码地址获取】web.Java代码 ...
spring mvc+mybatis+多数据源切换 选取oracle,mysql作为例子切换数据源。oracle为默认数据源,在测试的action中,进行mysql和oracle的动态切换。 dispatcher.Java代码
- <mvc:annotation-driven />
- <context:component-scan base-package="com.trac" />
-
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
-
- <!-- freemarker config -->
- <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
- <property name="freemarkerVariables">
- <map>
- <entry key=""fm
- </map>
- </property>
- <property name="freemarkerSettings">
- <props>
- <prop key="defaultEncoding">UTF-8</prop>
- </props>
- </property>
- </bean>
-
- <bean id="fmclass="freemarker.template.utility.
-
- <!-- View resolvers can also be configured with ResourceBundles or
- If you need different view resolving based on Locale, you have to use the
- resource bundle resolver. -->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="exposeRequestAttributes" value="true" />
- <property name="exposeSessionAttributes" value="true" />
- <property name="exposeSpringMacroHelpers" value="true" />
- <property name="contentType" value="text/html;charset=UTF-8" />
- <property name="cache" value="true" />
- <property name="prefix" value="" />
- <property name="suffix" value=".ftl" />
- </bean>
applicationContext.Java代码
- <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- </bean>
-
- <bean id="mySqlDataSource" parent="parentDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
- <property name="username" value="root"></property>
- <property name="password" value="root"></property>
- </bean>
-
- <bean id="oracleDataSource" parent="parentDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
- <property name="url" value="jdbc:oracle:thin:@10.16.17.40:1531:addb"></property>
- <property name="username" value="trac"></property>
- <property name="password" value="trac"></property>
- </bean>
-
- <bean id="dataSource" class="com.trac.dao.datasource.DataSources">
- <property name="targetDataSources">
- <map key-type="java.lang.String">
- <entry value-ref="mySqlDataSource" key="MYSQL"></entry>
- <entry value-ref="oracleDataSource" key="ORACLE"></entry>
- </map>
- </property>
- <property name="defaultTargetDataSource" ref="oracleDataSource"></property>
- </bean>
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
-
- <!-- 创建SqlSessionFactory,同时指定数据源和mapper -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations" value="classpath*:com/trac/ibatis/dbcp/*.
- </bean>
-
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory" />
- </bean>
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.trac.dao" />
- </bean>
配置 parentDataSource 的父bean.再配置多个数据源继承这个父bean,对driverClass,url,username,password,等数据源连接参数进行各自的重写。例如 mySqlDataSource ,在 DataSources bean中注入所有要切换的数据源,并且设置默认的数据源。 DataSourceInstances.java Java代码
- public class DataSourceInstances{
- public static final String MYSQL="MYSQL";
- public static final String ORACLE="ORACLE";
- }
-
- 定义数据源的标识, 和applicationContext.
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:Spring+spring mvc+mybatis 多数据源切换
关键词:Spring
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。