以下のサイトを参考に、できるようになりました。
http://m-namiki.hatenablog.jp/entries/2011/11/24
application.properties (使いません)
lang
1 #DB(postgre)
2 #spring.database.driverClassName=org.postgresql.Driver
3 #spring.datasource.url=jdbc:postgresql://localhost:5432/aaa
4 #spring.datasource.username=aaa
5 #spring.datasource.password=aaa
6
7 #DB(oracle)
8 #spring.database.driverClassName=oracle.jdbc.driver.OracleDriver
9 #spring.datasource.url=jdbc:oracle:thin:@aaa:1521/aa
10 #spring.datasource.username=aaa
11 #spring.datasource.password=aaa
pom.xml (変更なし)
以下のクラスを新たに定義
lang
1 /*****************/
2 /** 接続先の定義 */
3 /*****************/
4 public enum SchemaType {
5 /** Oracle接続用のデータソースキー */
6 ORACLE,
7 /** PostgreSql接続用のデータソースキー */
8 POSTGRES
9 }
10
11 /***************************/
12 /** 接続先を保持するクラス */
13 /***************************/
14 public class SchemaContextHolder {
15 private static ThreadLocal<SchemaType> contextHolder = new ThreadLocal<SchemaType>();
16 /**
17 * スレッドローカルに対象スキーマを設定します。
18 *
19 * @param type
20 * 対象スキーマの種類
21 */
22 public static void setSchemaType(SchemaType type) {
23 contextHolder.set(type);
24 }
25 /**
26 * 対象スキーマを返却します。
27 *
28 * @return 対象スキーマ
29 */
30 public static SchemaType getSchemaType() {
31 return contextHolder.get();
32 }
33 /**
34 * スレッドローカルを空にします。
35 */
36 public static void clear() {
37 contextHolder.remove();
38 }
39 }
40
41 /**********************************/
42 /** データソースを返却するクラス */
43 /**********************************/
44 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
45
46 public class DynamicRoutingDataSourceResolver extends AbstractRoutingDataSource {
47
48 @Override
49 protected Object determineCurrentLookupKey() {
50
51 if (SchemaContextHolder.getSchemaType() == null) {
52 } else if(SchemaContextHolder.getSchemaType() == SchemaType.POSTGRES) {
53 return "dataSource1";
54 } else if(SchemaContextHolder.getSchemaType() == SchemaType.ORACLE) {
55 return "dataSource2";
56 }
57 return "dataSource1";
58 }
59 }
60
61 /*********************************/
62 /** データソースを定義したクラス */
63 /*********************************/
64 import java.util.HashMap;
65 import java.util.Map;
66 import javax.sql.DataSource;
67 import org.springframework.context.annotation.Bean;
68 import org.springframework.context.annotation.Configuration;
69
70 @Configuration
71 public class DatasourceConfig {
72
73 @Bean()
74 public DataSource dataSource1() {
75 org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
76 ds.setDriverClassName("org.postgresql.Driver");
77 ds.setUrl("jdbc:postgresql://posgredb:5432/aaa");
78 ds.setUsername("posgreuser");
79 ds.setPassword("posgrepass");
80 return ds;
81 }
82
83 @Bean()
84 public DataSource dataSource2() {
85 org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
86 ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
87 ds.setUrl("jdbc:oracle:thin:@oradb:1521/aaa");
88 ds.setUsername("orauser");
89 ds.setPassword("orapass");
90 return ds;
91 }
92
93 @Bean()
94 public DynamicRoutingDataSourceResolver dataSource() {
95 DynamicRoutingDataSourceResolver resolver = new DynamicRoutingDataSourceResolver();
96
97 Map<Object, Object> dataSources = new HashMap<Object,Object>();
98 dataSources.put("dataSource1", dataSource1());
99 dataSources.put("dataSource2", dataSource2());
100
101 resolver.setTargetDataSources(dataSources);
102
103 return resolver;
104 }
105 }
使い方
lang
1 /*****************/
2 /** メインクラス */
3 /*****************/
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
6 import org.springframework.context.annotation.ComponentScan;
7 import org.springframework.context.annotation.Configuration;
8 import org.springframework.context.annotation.Jsr330ScopeMetadataResolver;
9
10 @Configuration
11 // TODO データソース動的切替え(scopeResolver = Jsr330ScopeMetadataResolver.class)
12 @ComponentScan(scopeResolver = Jsr330ScopeMetadataResolver.class)
13 @EnableAutoConfiguration
14 public class MyApplication {
15 public static void main(String[] args) {
16 SpringApplication.run(M3SApplication.class, args);
17 }
18 }
19 /***********************/
20 /** コントローラクラス */
21 /***********************/
22 import java.util.List;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Controller;
25 import org.springframework.web.bind.annotation.RequestMapping;
26 import org.springframework.web.bind.annotation.RequestMethod;
27 import org.springframework.web.servlet.ModelAndView;
28
29 @Controller
30 public class TopController {
31
32 /** Topサービス */
33 @Autowired
34 TopService topService;
35
36 @RequestMapping(value="/getOracle", method={RequestMethod.POST, RequestMethod.GET})
37 public ModelAndView getOracle(){
38
39 // 接続先データソースにOracleを指定
40 SchemaContextHolder.setSchemaType(SchemaType.ORACLE);
41 // Oracleからデータ取得
42 List<Object[]> list = topService.getOracleData();
43
44 // 遷移先画面の設定
45 ModelAndView mv = new ModelAndView("oraclePage");
46 // 画面パラメータ設定
47 mv.addObject("oracleData",list);
48
49 return mv;
50 }
51
52 @RequestMapping(value="/getPostgres", method={RequestMethod.POST, RequestMethod.GET})
53 public ModelAndView getPostgres(){
54
55 // 接続先データソースにPostgresを指定
56 SchemaContextHolder.setSchemaType(SchemaType.POSTGRES);
57 // POSTGRESからデータ取得
58 List<Object[]> list = topService.getPostgresData();
59
60 // 遷移先画面の設定
61 ModelAndView mv = new ModelAndView("PostgresPage");
62 // 画面パラメータ設定
63 mv.addObject("postgresData",list);
64
65 return mv;
66 }
67 }
68 /*******************/
69 /** サービスクラス */
70 /*******************/
71 @Service
72 @Transactional
73 public class TopService {
74 /** Oracleデータ取得用リポジトリ */
75 @Autowired
76 IOracleRepository oracle_repository;
77
78 /** Postgresデータ取得用リポジトリ */
79 @Autowired
80 IPostgresRepository postgres_repository;
81
82 // Oracleデータ取得実行
83 public List<Object[]> getOracleData(){
84 return oracle_repository.findAll();
85 }
86 // Postgresデータ取得実行
87 public List<Object[]> getPostgresData(){
88 return postgres_repository.findByYMD("20141222","20141228");
89 }
90 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。