I have two databases, test and test1. Both databases have a table named TEST_ONE.
The configuration of applicationContext. xml is as follows:
// Configure database test
<Bean id = "test"
Class = "org. springframework. jdbc. datasource. DriverManagerDataSource">
<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>
// Configure database test1
<Beanid = "test1"
Class = "org. springframework. jdbc. datasource. DriverManagerDataSource">
<Property name = "driverClassName" value = "com. mysql. jdbc. Driver">
</Property>
<Property name = "url" value = "jdbc: mysql: // localhost: 3306/test1"> </property>
<Property name = "username" value = "root"> </property>
<Property name = "password" value = "root"> </property>
</Bean>
// Integrate two data sources and specify the data source management class
<Beanid = "dataSource" class = "com. keith. dataSource. DynamicDataSource">
<Propertyname = "targetDataSources">
<Mapkey-type = "java. lang. String">
<Entry key = "test" value-ref = "test"/>
<Entry key = "test1" value-ref = "test1"/>
</Map>
</Property>
// Set the default data source configuration
<Property name = "defaultTargetDataSource" ref = "test"/>
</Bean>
<Bean id = "sessionFactory"
Class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">
<Property name = "dataSource">
<Ref bean = "dataSource"/>
</Property>
<Propertyname = "hibernateProperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. MySQLDialect
</Prop>
</Props>
</Property>
<Propertyname = "mappingResources">
<List>
<Value> com/keith/vo/TestOne. hbm. xml </value> </list>
</Property>
</Bean>
Tool class for data source switching:
Public class DBContextHolder {
// Database test
Public static final StringDB_TEST = "test ";
// Database test1
Public static final StringDB_TEST_1 = "test1 ";
Private static finalThreadLocalCON_LOCAL = new ThreadLocal ();
Public static voidsetDB (String type ){
CON_LOCAL.set (type );
}
Public static StringgetDB (){
Return (String) CON_LOCAL.get ();
}
Public static voidclearDB (){
CON_LOCAL.remove ();
}
}
// This class is used to manage the data source, in the configuration file
Public class DynamicDataSource extendsAbstractRoutingDataSource {
@ Override
ProtectedObject determineCurrentLookupKey (){
// TODOAuto-generated method stub
Return DBContextHolder. getDB ();
}
}
// Compile the test class
Public class Test {
Publicstatic void main (String [] args ){
ApplicationContext context = newClassPathXmlApplicationContext ("applicationContext. xml ");
// Dynamically set the data source
DBContextHolder. setDB (DBContextHolder. DB_TEST_1 );
SessionFactory sf = (SessionFactory) context. getBean ("sessionFactory ");
Session s = sf. openSession ();
Transactiontr = s. getTransaction ();
Tr. begin ();
TestOneone = new TestOne ();
One. setName ("aaa ");
S. save (one );
Tr. commit ();
Sf. close ();
}
}