|
@@ -0,0 +1,61 @@
|
|
|
|
|
+package com.malk.minglei.config;
|
|
|
|
|
+
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
|
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
|
|
|
|
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
|
|
+
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 数据字典 Oracle 数据源和具名参数 JDBC 模板配置。
|
|
|
|
|
+ */
|
|
|
|
|
+@Configuration
|
|
|
|
|
+@EnableConfigurationProperties(DictionaryProperties.class)
|
|
|
|
|
+public class DictionaryConfiguration {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 仅在配置数据源地址后创建数据字典专用 Oracle 数据源。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param properties 数据字典配置项
|
|
|
|
|
+ * @return 数据字典数据源
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean("dictionaryDataSource")
|
|
|
|
|
+ @ConditionalOnExpression("'${minglei.dictionary.datasource.url:}' != ''")
|
|
|
|
|
+ public DataSource dictionaryDataSource(DictionaryProperties properties) {
|
|
|
|
|
+ DictionaryProperties.DataSourceProperties datasource = properties.getDatasource();
|
|
|
|
|
+ DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
|
|
+ dataSource.setUrl(datasource.getUrl());
|
|
|
|
|
+ if (hasText(datasource.getDriverClassName())) {
|
|
|
|
|
+ dataSource.setDriverClassName(datasource.getDriverClassName());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (hasText(datasource.getUsername())) {
|
|
|
|
|
+ dataSource.setUsername(datasource.getUsername());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (datasource.getPassword() != null) {
|
|
|
|
|
+ dataSource.setPassword(datasource.getPassword());
|
|
|
|
|
+ }
|
|
|
|
|
+ return dataSource;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建供具体字典 SQL 使用的具名参数 JDBC 模板。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param dataSource 数据字典数据源
|
|
|
|
|
+ * @return 数据字典 JDBC 模板
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean("dictionaryNamedParameterJdbcTemplate")
|
|
|
|
|
+ @ConditionalOnBean(name = "dictionaryDataSource")
|
|
|
|
|
+ public NamedParameterJdbcTemplate dictionaryNamedParameterJdbcTemplate(
|
|
|
|
|
+ @Qualifier("dictionaryDataSource") DataSource dataSource) {
|
|
|
|
|
+ return new NamedParameterJdbcTemplate(dataSource);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean hasText(String value) {
|
|
|
|
|
+ return value != null && !value.trim().isEmpty();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|