Dynamic switching of data sources on the Internet can not meet my current application, and switching back and forth will cause performance problems and crash. And it's not a good way to switch notes on a certain method. So think about it at the top level. Here is my configuration:
Initializing multiple spring-mybatis.xml files in web.xml
< context param > <! -- global environment parameter initialization -- > < param name > contextconfiglocation < / param name > <! -- parameter name -- > < param value > classpath: spring-mybatis.xml, classpath: spring-mybatis1.xml < / param value > <! -- parameter value, comma separated -- > </context-param>
Then there is spring-mybatis.xml, which is simple, convenient and has no performance problems.
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- Automatic scanning --> <context:component-scan base-package="com.tzc.webapi.service" /> <!-- Import profile --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!--slave library --> <bean id="DataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${slave.jdbc.driverClassName}" /> <property name="url" value="${slave.jdbc.url}" /> <property name="username" value="${slave.jdbc.username}" /> <property name="password" value="${slave.jdbc.password}" /> <!-- Configure initialization size, min, Max --> <property name="initialSize" value="${initialSize}" /> <property name="minIdle" value="${minIdle}" /> <property name="maxActive" value="${maxActive}" /> <!-- Configure the timeout time for getting connection waiting --> <property name="maxWait" value="${maxWait}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="DataSource1" /> <!-- Automatic scanning mapping.xml file --> <property name="typeAliasesPackage" value="com.tzc.webapi.bean"></property> <property name="mapperLocations" value="classpath:slave/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.tzc.webapi.dao.user"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="DataSource" /> <!-- Automatic scanning mapping.xml file --> <property name="typeAliasesPackage" value="com.tzc.webapi.bean"></property> <property name="mapperLocations" value="classpath:master/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.tzc.webapi.dao.book"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"></property> </bean> <!-- (transaction management)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="DataSource1" /> </bean> <!-- Use annotation Annotation mode configuration transaction --> <!--<tx:annotation-driven transaction-manager="transactionManager"/>--> <!--<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>--> <!--<bean name="myAspectJ" class="com.tzc.webapi.dbRouting.DynamicDataSourceAspect"/>--> </beans> spring-mybatis1.xml Configuration `` ```. <?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- Automatic scanning --> <context:component-scan base-package="com.tzc.webapi.service" /> <!-- Import profile --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!--master library--> <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${master.jdbc.driverClassName}" /> <property name="url" value="${master.jdbc.url}" /> <property name="username" value="${master.jdbc.username}" /> <property name="password" value="${master.jdbc.password}" /> <property name="initialSize" value="${initialSize}" /> <property name="minIdle" value="${minIdle}" /> <property name="maxActive" value="${maxActive}" /> <property name="maxWait" value="${maxWait}" /> </bean> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="DataSource" /> <!-- Automatic scanning mapping.xml file --> <property name="typeAliasesPackage" value="com.tzc.webapi.bean"></property> <property name="mapperLocations" value="classpath:master/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.tzc.webapi.dao.book"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"></property> </bean> <!-- (transaction management)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="DataSource" /> </bean> </beans>
Test:
The test passed. From different data sources.
Conclusion: in fact, different data sources scan different mapper files and different Daos. Get the data in the destination database. ok