|
@@ -1,11 +1,16 @@
|
|
|
package com.sundata.config;
|
|
|
|
|
|
-import javax.sql.DataSource;
|
|
|
-
|
|
|
+import cn.hutool.setting.Setting;
|
|
|
+import com.sundata.datasource.DataSources;
|
|
|
+import com.sundata.datasource.EncryptedDatasource;
|
|
|
+import com.zaxxer.hikari.HikariConfig;
|
|
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
@@ -15,36 +20,58 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
|
|
-import com.sundata.datasource.EncryptedDatasource;
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
|
@Configuration
|
|
|
@MapperScan(basePackages = "com.sundata.**.mapper", sqlSessionTemplateRef = "sqlSessionTemplate")
|
|
|
public class DefaultDataSourceConfig {
|
|
|
- @Value("${spring.datasource.encrypted:false}")
|
|
|
- private Boolean encrypted;
|
|
|
-
|
|
|
- @Bean
|
|
|
- @Primary
|
|
|
- @ConfigurationProperties(prefix = "spring.datasource")
|
|
|
- public DataSource dataSource() {
|
|
|
- if (this.encrypted) return new EncryptedDatasource();
|
|
|
- return DataSourceBuilder.create().build();
|
|
|
- }
|
|
|
-
|
|
|
- @Bean
|
|
|
- public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource datasource)
|
|
|
- throws Exception {
|
|
|
- SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
|
|
- bean.setDataSource(datasource);
|
|
|
- bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
|
|
|
- bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/sundata/**/mybatis/*.xml"));
|
|
|
- return bean.getObject();// mybatis xml所在
|
|
|
- }
|
|
|
-
|
|
|
- @Bean
|
|
|
- @Primary
|
|
|
- public SqlSessionTemplate sqlSessionTemplate(
|
|
|
- @Qualifier("sqlSessionFactory") SqlSessionFactory sessionfactory) {
|
|
|
- return new SqlSessionTemplate(sessionfactory);
|
|
|
- }
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(DefaultDataSourceConfig.class);
|
|
|
+ @Value("${spring.datasource.encrypted:false}")
|
|
|
+ private Boolean encrypted;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @Primary
|
|
|
+ @ConfigurationProperties(prefix = "spring.datasource")
|
|
|
+ public DataSource dataSource() {
|
|
|
+ if (this.encrypted) return new EncryptedDatasource();
|
|
|
+ return DataSourceBuilder.create().build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource datasource)
|
|
|
+ throws Exception {
|
|
|
+ SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
|
|
+ bean.setDataSource(datasource);
|
|
|
+ bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
|
|
|
+ bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/sundata/**/mybatis/*.xml"));
|
|
|
+ return bean.getObject();// mybatis xml所在
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @Primary
|
|
|
+ public SqlSessionTemplate sqlSessionTemplate(
|
|
|
+ @Qualifier("sqlSessionFactory") SqlSessionFactory sessionfactory) {
|
|
|
+ return new SqlSessionTemplate(sessionfactory);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public DataSources initOtherDatasource() {
|
|
|
+ DataSources dataSources = new DataSources();
|
|
|
+ Setting setting = new Setting("OtherDBConfig.setting");
|
|
|
+// String group = "GBaseDB";
|
|
|
+ setting.getGroups().forEach(group -> {
|
|
|
+ HikariConfig config = new HikariConfig();
|
|
|
+ config.setDriverClassName(setting.getByGroup("driver", group));
|
|
|
+ config.setJdbcUrl(setting.getByGroup("url", group));
|
|
|
+ config.setUsername(setting.getByGroup("user", group));
|
|
|
+ config.setPassword(setting.getByGroup("pass", group));
|
|
|
+ config.setMaximumPoolSize(5);
|
|
|
+ config.setMinimumIdle(1);
|
|
|
+ config.setAutoCommit(true);//自动提交
|
|
|
+ HikariDataSource ds = new HikariDataSource(config);
|
|
|
+ dataSources.setDataSource(group, ds);
|
|
|
+ });
|
|
|
+
|
|
|
+ return dataSources;
|
|
|
+ }
|
|
|
}
|