大家在使用的时候,只要将例子的设置密码的方法重新编写即可
package com.wins.core.config.mybatis;
import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
/**
* @author:shiyx
* @date:2021/12/15
* @description:
*/
@Configuration
public class DataSourceInit {
private static final Logger log = LoggerFactory.getLogger(DataSourceInit.class);
@Value("${spring.profiles.active}")
private String runEnv;
/**
* 数据源自定义加载
*
* @param properties 数据源配置(某些idea中properties字段可能会爆红,忽略即可,不影响使用 )
* @return DynamicDataSourceProvider
*/
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider(DynamicDataSourceProperties properties) {
Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
DataSourceProperty masterDataSourceProperty = datasourceMap.get("master");
String passWord = "";
// TODO 获取密码,密码解密等操作
masterDataSourceProperty.setPassword(passWord);
// 加载主数据源(yml中配置的数据源)
return new AbstractJdbcDataSourceProvider(masterDataSourceProperty.getDriverClassName(),
masterDataSourceProperty.getUrl(),
masterDataSourceProperty.getUsername(),
masterDataSourceProperty.getPassword()) {
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
return datasourceMap;
}
};
}
}
附applicationn-dev数据库模块配置
spring:
datasource:
druid:
stat-view-servlet:
enabled: true
loginUsername: root
loginPassword: abcd1234
allow:
web-stat-filter:
enabled: true
dynamic:
druid: # 全局druid参数,绝大部分值和默认保持一致。(现已支持的参数如下,不清楚含义不要乱设置)
# 连接池的配置信息
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
datasource:
master:
url: jdbc:mysql://10.102.17.110:3306/wins_home_db_prod?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
username: root
password:
driver-class-name: com.mysql.jdbc.Driver