|
|
@@ -0,0 +1,120 @@
|
|
|
+package com.shkpr.service.customgateway.core.config;
|
|
|
+
|
|
|
+import com.shkpr.service.customgateway.core.io.YamlPropertySourceFactory;
|
|
|
+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.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.PropertySource;
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 主要的DataSource配置
|
|
|
+ *
|
|
|
+ * @author 欧阳劲驰
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@MapperScan(basePackages = "com.shkpr.service.customgateway.**.mapper", sqlSessionTemplateRef = "mainSqlSessionTemplate")
|
|
|
+@PropertySource(value = "file:${global.sql-config-path}", ignoreResourceNotFound = true, encoding = "UTF-8", factory = YamlPropertySourceFactory.class)
|
|
|
+public class DataSourceConfig {
|
|
|
+ @Value("${spring.datasource.data.driver-class-name:org.postgresql.Driver}")
|
|
|
+ private String driveClass;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.jdbc-url:}")
|
|
|
+ private String url = "";
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.username:}")
|
|
|
+ private String username = "";
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.password:}")
|
|
|
+ private String password = "";
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.maximum-pool-size:200}")
|
|
|
+ private Integer maxPoolSize;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.minimum-idle:1}")
|
|
|
+ private Integer minIdle;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.connection-test-query:}")
|
|
|
+ private String connectionTestQuery;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.max-lifetime:120000}")
|
|
|
+ private Long maxLifetime;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.idle-timeout:30000}")
|
|
|
+ private Long idleTimeout;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.connection-timeout:30000}")
|
|
|
+ private Long connectionTimeout;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.validation-timeout:30000}")
|
|
|
+ private Long validTimeout;
|
|
|
+
|
|
|
+ @Value("${spring.datasource.data.init-failed-timeout:-1}")
|
|
|
+ private Long initFailedTimeout;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return DataSource
|
|
|
+ */
|
|
|
+ @Bean(name = "mainDatasource")
|
|
|
+ public DataSource mainDataSource() {
|
|
|
+ //构建DataSource
|
|
|
+ HikariDataSource dataSource = DataSourceBuilder.create()
|
|
|
+ .type(HikariDataSource.class)
|
|
|
+ .url(url)
|
|
|
+ .username(username)
|
|
|
+ .password(password)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ dataSource.setDriverClassName(driveClass);
|
|
|
+ dataSource.setUsername(username);
|
|
|
+ dataSource.setPassword(password);
|
|
|
+
|
|
|
+ dataSource.setMaximumPoolSize(maxPoolSize);
|
|
|
+ dataSource.setMinimumIdle(minIdle);
|
|
|
+ dataSource.setConnectionTestQuery(connectionTestQuery);
|
|
|
+ dataSource.setMaxLifetime(maxLifetime);
|
|
|
+ dataSource.setIdleTimeout(idleTimeout);
|
|
|
+ dataSource.setConnectionTimeout(connectionTimeout);
|
|
|
+ dataSource.setValidationTimeout(validTimeout);
|
|
|
+ dataSource.setInitializationFailTimeout(initFailedTimeout);
|
|
|
+
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Bean("mainSqlSessionFactory")
|
|
|
+ public SqlSessionFactory mainSqlSessionFactoryBean(@Qualifier("mainDatasource") DataSource dataSource) throws Exception {
|
|
|
+ SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
|
|
|
+ sessionFactoryBean.setDataSource(dataSource);
|
|
|
+ sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
|
|
|
+ .getResources("classpath:mapper/*.xml"));
|
|
|
+ org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
|
|
|
+ configuration.setCallSettersOnNulls(true); //数据库中字段值为null时也要求返回
|
|
|
+ //驼峰映射
|
|
|
+ configuration.setMapUnderscoreToCamelCase(true); //开启驼峰映射
|
|
|
+ configuration.setCacheEnabled(false);
|
|
|
+ sessionFactoryBean.setConfiguration(configuration);
|
|
|
+ return sessionFactoryBean.getObject();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean("mainSqlSessionTemplate")
|
|
|
+ public SqlSessionTemplate mainSqlSessionTemplate(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sessionFactory) {
|
|
|
+ return new SqlSessionTemplate(sessionFactory);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "mainDbTransactionManager")
|
|
|
+ public DataSourceTransactionManager mainDbTransactionManager(@Qualifier("mainDatasource") DataSource dataSource) {
|
|
|
+ return new DataSourceTransactionManager(dataSource);
|
|
|
+ }
|
|
|
+}
|