|
|
@@ -0,0 +1,75 @@
|
|
|
+package com.test;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.annotation.Import;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import com.util.BasesSource.DynamicDataSource;
|
|
|
+import com.util.BasesSource.DynamicDataSourceContextHolder;
|
|
|
+import com.util.BasesSource.DynamicDataSourceRegister;
|
|
|
+
|
|
|
+
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest
|
|
|
+//@SpringApplicationConfiguration(classes=Application.class)
|
|
|
+@Import({DynamicDataSourceRegister.class})
|
|
|
+public class TestDataSource{
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApplicationContext app;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DynamicDataSourceRegister dataSourceRegister;
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate JdbcTemplate;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+ System.out.println("begin==========================");
|
|
|
+ addDataSource();
|
|
|
+ System.out.println("end==========================");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void testQuery(){
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType("UAS");
|
|
|
+ List<Map<String, Object>> list = JdbcTemplate.queryForList("SELECT EN_NAME FROM ENTERPRISE");
|
|
|
+ System.out.println("UAS:"+list);
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType("UAS_DEV");
|
|
|
+ List<Map<String, Object>> list2 = JdbcTemplate.queryForList("SELECT EN_NAME FROM ENTERPRISE");
|
|
|
+ System.out.println("UAS_DEV:"+list2);
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType("UAS_TEST");
|
|
|
+ List<Map<String, Object>> list3 = JdbcTemplate.queryForList("SELECT EN_NAME FROM ENTERPRISE");
|
|
|
+ System.out.println("UAS_TEST:"+list3);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addDataSource(){
|
|
|
+
|
|
|
+ //额外的数据源
|
|
|
+ Map<String, Object> dsMap = new HashMap<>();
|
|
|
+ dsMap.put("driver", "oracle.jdbc.driver.OracleDriver");
|
|
|
+ dsMap.put("url", "jdbc:oracle:thin:@192.168.253.6:1521:orcl");
|
|
|
+ dsMap.put("username", "UAS_TEST");
|
|
|
+ dsMap.put("password", "select!#%*(");
|
|
|
+ Map<Object, Object> target = dataSourceRegister.getTargetDataSources();
|
|
|
+ if(target.size() == 0){
|
|
|
+ target.putAll(dataSourceRegister.getSlaveDataSources());
|
|
|
+ }
|
|
|
+ target.put("UAS_TEST", dataSourceRegister.buildDataSource(dsMap));
|
|
|
+ DynamicDataSource datasource=(DynamicDataSource)app.getBean("dataSource");
|
|
|
+ datasource.setTargetDataSources(target);
|
|
|
+ datasource.afterPropertiesSet();
|
|
|
+
|
|
|
+ DynamicDataSourceContextHolder.dataSourceIds.add("UAS_TEST");
|
|
|
+
|
|
|
+ testQuery();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|