I also consulted a number of resource information on the Internet, the summary is as follows:
First, JDBC configuration, I use the two databases are Oracle, users can according to their own situation to adjust the configuration information inside
Jdbc.driver=oracle.jdbc.driver.oracledriver
JDBC.URL1=JDBC\:ORACLE\:THIN\:@192.168.1.88\:1521\:ORCL
Jdbc.username1=test1
Jdbc.password1=password
JDBC.URL2=JDBC\:ORACLE\:THIN\:@192.168.1.88\:1521\:ORCL
jdbc.username2=test2
jdbc.password2=password
jdbc.acquireincrement=3
jdbc.initialpoolsize=3
jdbc.minpoolsize=2
jdbc.maxpoolsize=100
jdbc.maxidletime=600
jdbc.maxstatements=100
jdbc.numhelperthreads=10
jdbc.idleconnectiontestperiod=900
Second, spring data source configuration
<!--configuration Properties start--> <bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.confi" G.preferencesplaceholderconfigurer "> <!--<property name=" Systempropertiesmodename "value=" SYSTEM_ Properties_mode_override "/> <property name=" Ignoreresourcenotfound value= "false"/>--> <property N Ame= "Locations" > <list> <value>classpath:config/jdbc.properties</value> </list> ;/property> </bean> <!--configuration Properties End--> <!--Configure the first datasource data source start--> <bean id= "C3p0dat ASource1 "class=" Com.mchange.v2.c3p0.ComboPooledDataSource "destroy-method=" Close "> <property name=" Driverclass "value=" ${jdbc.driver} "/> <property name=" Jdbcurl "value=" ${jdbc.url1} "/> <property name=" us Er "value=" ${jdbc.username1} "/> <property name=" password "value=" ${jdbc.password1} "/> <property name=" ac Quireincrement "value=" ${jdbc.acquireincrement} "/> <property name= "initialpoolsize" value= "${jdbc.initialpoolsize}"/> <property name= "minPoolSize" value = "${jdbc.minpoolsize}"/> <property name= "maxpoolsize" value= "${jdbc.maxpoolsize}"/> <property name= "max IdleTime "value=" ${jdbc.maxidletime} "/> <property name=" maxstatements "value=" ${jdbc.maxstatements} "/> ;p roperty name= "numhelperthreads" value= "${jdbc.numhelperthreads}"/> <property name= " Idleconnectiontestperiod "value=" ${jdbc.idleconnectiontestperiod} "/> </bean> <!--Configure the first datasource data source end --> <!--Configure the first DataSource data source to start--> <bean id= "C3p0datasource2" class= "Com.mchange.v2.c3p0.ComboPooledDataS" Ource "destroy-method=" Close "> <property name=" driverclass "value=" ${jdbc.driver} "/> <property name=" JD Bcurl "value=" ${jdbc.url2} "/> <property name=" user "value=" ${jdbc.username2} "/> <property name=" password "Value=" ${jdbc.password2} "/> <property name=" AcquIreincrement "value=" ${jdbc.acquireincrement} "/> <property name=" initialpoolsize "value=" ${ Jdbc.initialpoolsize} "/> <property name=" minpoolsize "value=" ${jdbc.minpoolsize} "/> <property name=" max Poolsize "value=" ${jdbc.maxpoolsize} "/> <property name=" maxidletime "value=" ${jdbc.maxidletime} "/> <pro Perty name= "maxstatements" value= "${jdbc.maxstatements}"/> <property name= "Numhelperthreads" value= Jdbc.numhelperthreads} "/> <property name=" idleconnectiontestperiod "value=" ${jdbc.idleconnectiontestperiod} "/> </bean> <!--Configure the first datasource data source end--> <!--Configure the data source switch tool class, which needs to be written by itself, followed by an introduction to--> <bean id="
Dynamicdatasource "class=" Com.odon.datasource.DynamicDataSource "> <property name=" targetdatasources "> <map key-type= "java.lang.String" > <entry value-ref= "C3p0datasource1" key= "C3p0data" Source1 "></entry> <entry value-ref="C3p0datasource2" key= "C3p0datasource2" ></entry> </map> </property> <property name= "Defaulttargetdatasource" ref= "C3p0datasource1" > <!--Configure the default data source--> T;/property> </bean> <!--create sqlsessionfactory start--> <bean id= "Sqlsessionfactory" O Rg.mybatis.spring.SqlSessionFactoryBean "> <!--referencing MyBatis profile--> <property name=" configlocation "value=" Classpath:config/mybatis-config.xml "/> <!--referencing data source configuration Here's what you need to be aware of. The tool class--> <property name=" DataSource "ref = "Dynamicdatasource"/> <!--reference Mapper profile--> <property name= "mapperlocations" value= "classpath:com/odon/" Mapper/*.xml "/> </bean> <!--create Sqlsessionfactory end--> <!--automatic Assembly DAO start--> <bean class=" org.m Ybatis.spring.mapper.MapperScannerConfigurer "> <property name=" basepackage "value=" Com.odon.dao " > </bean>;! --Automatically assemble DAO end--> <!--enable Spring annotation support--> <context:annotation-config/> <!--automatic scanning (automatic injection)--> <con Text:component-scan base-package= "Com.odon.service. * "/> <!--configuration transaction manager started--> <bean id=" Txmanager "class=" Org.springframework.jdbc.datasource.DataSourceTrans ActionManager "> <property name=" dataSource "ref=" C3p0datasource1 "></property> </bean> <!-- Configure the transaction manager to end the--> <!--basic transaction definition, using TransactionManager for transaction management, the default get*,find* method transaction is read-only--> <tx:advice id= "Txadv Ice "transaction-manager=" Txmanager "> <tx:attributes> <tx:method name=" find* "read-only=" true "/> & Lt;tx:method name= "get*" read-only= "true"/> <tx:method name= "update*" propagation= "REQUIRED"/> <tx:me Thod name= "delete*" propagation= "REQUIRED"/> <tx:method name= "save*" propagation= "REQUIRED"/> <tx:meth OD name= "*" propagation= "REQUIRED"/> </tx:attributes> </tx:advice>;! --The tangent of things defines AOP in ASPECTJ way the first * represents all return value types The second * represents all the classes and the third * represents all the methods of the class, the last one.
Represents all the parameters. --> <aop:config> <aop:pointcut id= "interceptorpointcuts expression=" Execution (* com.odon.service.imp l.*impl.* (..)) " /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "interceptorpointcuts"/> </aop:config>
Third, data source switch tool class
Package Com.odon.datasource;
Import Org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* @author Zhangqifeng
* @date: 2017-11-1 pm 3:48:38
* @version:
*/public
class Dynamicdatasource extends abstractroutingdatasource{
@Override
protected Object Determinecurrentlookupkey ( ) {return
datasourcecontextholder.getdbtype ();
}
}
Four, data source switching method
Package Com.odon.datasource;
/**
* @author Zhangqifeng
* @date: 2017-11-1 pm 3:49:23
* @version:
*/public
class Datasourcecontextholder {
private static final threadlocal<string> Contextholder = new threadlocal<string > ();
Set the data source you want to use public
static void Setdbtype (String dbType) {
contextholder.set (dbType);
}
Gets the data source public
static String Getdbtype () {return
contextholder.get ();
}
Clears the data source, using the default data source public
static void Cleardbtype () {
contextholder.remove ();
}
}
V. Data source Variable Class
Package Com.odon.datasource;
/**
* @author Zhangqifeng
* @date: 2017-11-1 pm 3:51:52
* @version:
*/public
class DatasourceType {
//Data source 1 public
static final String source_1 = "C3p0datasource1";
Data source 2 public
static final String source_2 = "C3p0datasource2";
}
Vi. Methods of Use
Package Com.odon.service.impl;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import Com.odon.service.OAFileService;
Import Com.odon.dao.OAFileMapper;
Import Com.odon.datasource.DataSourceContextHolder;
Import Com.odon.datasource.DataSourceType; /** * @author Zhangqifeng * @date: 2017-11-1 pm 5:01:17 * @version: */@Service ("Oafileservice") public class Oafile
Serviceimpl implements oafileservice{@Autowired private Oafilemapper oafilemapper; @Override public list<map<string,object>> selectoafilelist () {list<map<string,object>> List
= new Arraylist<map<string,object>> ();
map<string,object> _map = new hashmap<string,object> ();
try {datasourcecontextholder.setdbtype (datasourcetype.source_2);
List = Oafilemapper.selectoafilelist (_map); catch (ExceptIon e) {e.printstacktrace ();
} return list; }
}