1
1
package cn .com .hellowood .dynamicdatasource .configuration ;
2
2
3
- import org .springframework . beans . factory . annotation . Qualifier ;
3
+ import org .mybatis . spring . SqlSessionFactoryBean ;
4
4
import org .springframework .boot .autoconfigure .jdbc .DataSourceBuilder ;
5
5
import org .springframework .boot .context .properties .ConfigurationProperties ;
6
6
import org .springframework .context .annotation .Bean ;
7
7
import org .springframework .context .annotation .Configuration ;
8
- import org .springframework .context .annotation .Primary ;
9
8
10
9
import javax .sql .DataSource ;
11
10
import java .util .HashMap ;
14
13
/**
15
14
* Multiple DataSource Configurer
16
15
*
17
- * @Date 2017年08月15日 11:37
18
- * @Author HelloWood
19
- * @Email hellowoodes@gmail.com
16
+ * @author HelloWood
17
+ * @date 2017 -08-15 11:37
18
+ * @Email hellowoodes@gmail.com
20
19
*/
21
20
@ Configuration
22
21
public class DataSourceConfigurer {
23
22
24
23
/**
25
24
* master DataSource
26
25
*
27
- * @return
26
+ * @return data source
28
27
*/
29
28
@ Bean ("master" )
30
- @ Qualifier ("master" )
31
- @ Primary
32
29
@ ConfigurationProperties (prefix = "application.server.db.master" )
33
30
public DataSource master () {
34
31
return DataSourceBuilder .create ().build ();
@@ -37,53 +34,54 @@ public DataSource master() {
37
34
/**
38
35
* slave DataSource
39
36
*
40
- * @return
37
+ * @return data source
41
38
*/
42
39
@ Bean ("slave" )
43
- @ Qualifier ("slave" )
44
40
@ ConfigurationProperties (prefix = "application.server.db.slave" )
45
41
public DataSource slave () {
46
42
return DataSourceBuilder .create ().build ();
47
43
}
48
44
45
+ /**
46
+ * Dynamic data source.
47
+ *
48
+ * @return the data source
49
+ */
49
50
@ Bean ("dynamicDataSource" )
50
- @ Qualifier ("dynamicDataSource" )
51
- public DataSource dataSource () {
51
+ public DataSource dynamicDataSource () {
52
52
DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource ();
53
-
54
- dynamicRoutingDataSource .setDefaultTargetDataSource (slave ());
55
-
56
53
Map <Object , Object > dataSourceMap = new HashMap <>(2 );
57
54
dataSourceMap .put ("master" , master ());
58
55
dataSourceMap .put ("slave" , slave ());
56
+
57
+ // Set master datasource as default
58
+ dynamicRoutingDataSource .setDefaultTargetDataSource (master ());
59
+ // Set master and slave datasource as target datasource
59
60
dynamicRoutingDataSource .setTargetDataSources (dataSourceMap );
60
- DynamicDataSourceContextHolder .dataSourceIds .addAll (dataSourceMap .keySet ());
61
+
62
+ // To put datasource keys into DataSourceContextHolder to judge if the datasource is exist
63
+ DynamicDataSourceContextHolder .dataSourceKeys .addAll (dataSourceMap .keySet ());
61
64
return dynamicRoutingDataSource ;
62
65
}
63
- //
64
- // @Bean
65
- // public PlatformTransactionManager transactionManager(DataSource dynamicDataSource) {
66
- // return new DataSourceTransactionManager(dynamicDataSource);
67
- // }
68
- //
69
- // @Bean
70
- // @ConfigurationProperties(prefix = "mybatis")
71
- // public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dynamicDataSource) {
72
- // SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
73
- // sqlSessionFactoryBean.setDataSource(dynamicDataSource);
74
- // return sqlSessionFactoryBean;
75
- // }
76
- //
77
- // @Bean
78
- // public SqlSessionFactory sqlSessionFactory() throws Exception {
79
- // return sqlSessionFactoryBean(dataSource()).getObject();
80
- //// return sqlSessionFactoryBean.getObject();
81
- // }
82
- //
83
- // @Bean
84
- // public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
85
- // SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
86
- // return sqlSessionTemplate;
87
- // }
66
+
67
+ /**
68
+ * Sql session factory bean.
69
+ * Here to config datasource for SqlSessionFactory
70
+ * <p>
71
+ * You need to add @{@code @ConfigurationProperties(prefix = "mybatis")}, if you are using *.xml file,
72
+ * the {@code 'mybatis.type-aliases-package'} and {@code 'mybatis.mapper-locations'} should be set in
73
+ * {@code 'application.properties'} file, or there will appear invalid bond statement exception
74
+ *
75
+ * @return the sql session factory bean
76
+ */
77
+ @ Bean
78
+ @ ConfigurationProperties (prefix = "mybatis" )
79
+ public SqlSessionFactoryBean sqlSessionFactoryBean () {
80
+ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean ();
81
+ // Here is very important, if don't config this, will can't switch datasource
82
+ // put all datasource into SqlSessionFactoryBean, then will autoconfig SqlSessionFactory
83
+ sqlSessionFactoryBean .setDataSource (dynamicDataSource ());
84
+ return sqlSessionFactoryBean ;
85
+ }
88
86
}
89
87
0 commit comments