日期:2014-05-17 浏览次数:20678 次
public class DynamicDataSource extends AbstractRoutingDataSource {
/*
* 该方法必须要重写 方法是为了根据数据库标示符取得当前的数据库
*/
@Override
public Object determineCurrentLookupKey() {
DataSourceType dataSourceType= DataSourceContextHolder.getDataSourceType();
return dataSourceType;
}
@Override
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
super.setDataSourceLookup(dataSourceLookup);
}
@Override
public void setDefaultTargetDataSource(Object defaultTargetDataSource) {
super.setDefaultTargetDataSource(defaultTargetDataSource);
}
@Override
public void setTargetDataSources(Map targetDataSources) {
super.setTargetDataSources(targetDataSources);
}
}
@Test
public void test() {
try {
List<UserinfoEntity> list = userInfoService.getList(UserinfoEntity.class);
System.out.println(list.size());
String dataSourceName = "qqq";
String url = "jdbc:mysql://127.0.0.1:3306/mos_saas_test";
String username = "root";
String password = "root";
ConfigFileCreator.createConfigFile(dataSourceName, url, username, password);
DynamicLoadBean dynamicBeanLoad =(DynamicLoadBean)SpringContextUtil.getBean("dynamicLoadBean");
dynamicBeanLoad.loadBean("classpath:mos/qqq.xml");
// dynamicBeanLoad.registBean(dataSourceName, "parentDataSource");
DruidDataSource dds = (DruidDataSource) SpringContextUtil.getBean(dataSourceName);
DynamicDataSource ds = (DynamicDataSource)SpringContextUtil.getBean("dataSource");
// Map targetDataSources = new HashMap();
// targetDataSources.put(DataSourceType.qqq, dds);
//
// ds.setTargetDataSources(targetDataSources);
// DataSourceLookup dataSourceLookup = new MapDataSourceLookup();
// dataSourceLookup.getDataSource("qqq");
//
// ds.setDataSourceLookup(dataSourceLookup);
DataSourceContextHolder.setDataSourceType(DataSourceType.qqq);
List<UserinfoEntity> list2 = userInfoService.getList(UserinfoEntity.class);
System.out.println(list2.size());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}