|
|
@@ -0,0 +1,140 @@
|
|
|
+package com.usoftchina.saas.server.jdbc;
|
|
|
+
|
|
|
+import com.usoftchina.saas.jdbc.DynamicDataSource;
|
|
|
+import com.usoftchina.saas.jdbc.DynamicDataSourceRegister;
|
|
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
|
+import org.apache.ibatis.mapping.DatabaseIdProvider;
|
|
|
+import org.apache.ibatis.plugin.Interceptor;
|
|
|
+import org.apache.ibatis.session.ExecutorType;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
+import org.mybatis.spring.SqlSessionFactoryBean;
|
|
|
+import org.mybatis.spring.SqlSessionTemplate;
|
|
|
+import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
|
|
|
+import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
|
|
|
+import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
|
|
|
+import org.springframework.beans.factory.ObjectProvider;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
+import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.core.io.ResourceLoader;
|
|
|
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author yingp
|
|
|
+ * @date 2018/12/10
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@ConditionalOnClass({HikariDataSource.class})
|
|
|
+@ConditionalOnMissingBean({DataSource.class})
|
|
|
+@EnableConfigurationProperties({
|
|
|
+ DataSourceProperties.class,
|
|
|
+ MybatisProperties.class
|
|
|
+})
|
|
|
+public class DynamicDataSourceConfig {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DataSourceProperties properties;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MybatisProperties mybatisProperties;
|
|
|
+
|
|
|
+ @Primary
|
|
|
+ @Bean(name = "defaultDataSource")
|
|
|
+ public HikariDataSource defaultDataSource() {
|
|
|
+ HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
|
|
|
+ if (StringUtils.hasText(properties.getName())) {
|
|
|
+ dataSource.setPoolName(properties.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "dynamicDataSource")
|
|
|
+ public DynamicDataSource dynamicDataSource(@Qualifier("defaultDataSource") DataSource defaultDataSource) {
|
|
|
+ Map<Object, Object> targetDataSources = new HashMap<>(1);
|
|
|
+ targetDataSources.put("defaultDataSource", defaultDataSource);
|
|
|
+ DynamicDataSource dataSource = new DynamicDataSource();
|
|
|
+ dataSource.setDefaultTargetDataSource(defaultDataSource);
|
|
|
+ dataSource.setTargetDataSources(targetDataSources);
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource,
|
|
|
+ ObjectProvider<Interceptor[]> interceptorsProvider,
|
|
|
+ ResourceLoader resourceLoader,
|
|
|
+ ObjectProvider<DatabaseIdProvider> databaseIdProvider,
|
|
|
+ ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) throws Exception {
|
|
|
+ SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
|
|
|
+ factory.setDataSource(dataSource);
|
|
|
+ factory.setVfs(SpringBootVFS.class);
|
|
|
+ if (StringUtils.hasText(mybatisProperties.getConfigLocation())) {
|
|
|
+ factory.setConfigLocation(resourceLoader.getResource(mybatisProperties.getConfigLocation()));
|
|
|
+ }
|
|
|
+ org.apache.ibatis.session.Configuration configuration = mybatisProperties.getConfiguration();
|
|
|
+ if (configuration == null && !StringUtils.hasText(mybatisProperties.getConfigLocation())) {
|
|
|
+ configuration = new org.apache.ibatis.session.Configuration();
|
|
|
+ }
|
|
|
+ List<ConfigurationCustomizer> configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
|
|
|
+ if (configuration != null && !CollectionUtils.isEmpty(configurationCustomizers)) {
|
|
|
+ for (ConfigurationCustomizer customizer : configurationCustomizers) {
|
|
|
+ customizer.customize(configuration);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ factory.setConfiguration(configuration);
|
|
|
+ if (mybatisProperties.getConfigurationProperties() != null) {
|
|
|
+ factory.setConfigurationProperties(mybatisProperties.getConfigurationProperties());
|
|
|
+ }
|
|
|
+ if (!ObjectUtils.isEmpty(interceptorsProvider)) {
|
|
|
+ factory.setPlugins(interceptorsProvider.getIfAvailable());
|
|
|
+ }
|
|
|
+ if (databaseIdProvider != null) {
|
|
|
+ factory.setDatabaseIdProvider(databaseIdProvider.getIfAvailable());
|
|
|
+ }
|
|
|
+ if (StringUtils.hasLength(mybatisProperties.getTypeAliasesPackage())) {
|
|
|
+ factory.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
|
|
|
+ }
|
|
|
+ if (StringUtils.hasLength(mybatisProperties.getTypeHandlersPackage())) {
|
|
|
+ factory.setTypeHandlersPackage(mybatisProperties.getTypeHandlersPackage());
|
|
|
+ }
|
|
|
+ if (!ObjectUtils.isEmpty(mybatisProperties.resolveMapperLocations())) {
|
|
|
+ factory.setMapperLocations(mybatisProperties.resolveMapperLocations());
|
|
|
+ }
|
|
|
+
|
|
|
+ return factory.getObject();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
|
|
|
+ ExecutorType executorType = mybatisProperties.getExecutorType();
|
|
|
+ if (executorType != null) {
|
|
|
+ return new SqlSessionTemplate(sqlSessionFactory, executorType);
|
|
|
+ } else {
|
|
|
+ return new SqlSessionTemplate(sqlSessionFactory);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDataSource") DynamicDataSource dataSource) throws Exception {
|
|
|
+ return new DataSourceTransactionManager(dataSource);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public DynamicDataSourceRegister dataSourceRegister(@Qualifier("dynamicDataSource") DynamicDataSource dataSource) {
|
|
|
+ return new DynamicDataSourceRegister(properties, dataSource);
|
|
|
+ }
|
|
|
+}
|