1、配置文件
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@192.168.**.**:1521/ORCL
username: ****
publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMLqCO+tt55XuV/VkyRH8fr7AlSyONv3Ld3mRuf7yFUCjwk2YLp9Q2C/6pyUXtwUYmU/bMQC2vuTsMdmpsF8uv0CAwEAAQ==
password: gTh5ZBFPg717xz7oO+FK7n5Q1j68GR7OXxwPDdyrtD4SW8sMUWj0jIOeM5/MUQi9W8oHY1A1q6SoWsuzxJm08A==
#Druid 连接池通用配置
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
# sql 校验
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打开PSCache,并且指定每个连接上PSCache的大小
pool-prepared-statements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
max-pool-prepared-statement-per-connection-size: 20
filters: stat,config # wall 若开启 wall,会把 if 中的 and 判断为注入进行拦截
use-global-data-source-stat: true
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
web-stat-filter:
enabled: true
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
stat-view-servlet:
enabled: true
reset-enable: false
2、Java工具类
import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;
/**
* Druid
* @create: 2019-07-01 11:10
**/
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
@Data
public class DruidDatsSourceConfig {
private Logger logger = LoggerFactory.getLogger(DruidDatsSourceConfig.class);
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.url}")
private String type;
@Value("${spring.datasource.publicKey}")
private String publicKey;
@Value("${spring.datasource.druid.initial-size}")
private Integer initialSize;
@Value("${spring.datasource.druid.min-idle}")
private Integer minIdle;
@Value("${spring.datasource.druid.max-active}")
private Integer maxActive;
@Value("${spring.datasource.druid.max-wait}")
private Integer maxWait;
@Value("${spring.datasource.druid.time-between-eviction-runs-millis}")
private Integer timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.druid.min-evictable-idle-time-millis}")
private Integer minEvictableIdleTimeMillis;
@Value("${spring.datasource.druid.validation-query}")
private String validationQuery;
@Value("${spring.datasource.druid.test-while-idle}")
private Boolean testWhileIdle;
@Value("${spring.datasource.druid.test-on-borrow}")
private Boolean testOnBorrow;
@Value("${spring.datasource.druid.test-on-return}")
private Boolean testOnReturn;
@Value("${spring.datasource.druid.pool-prepared-statements}")
private Boolean poolPreparedStatements;
@Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}")
private Integer maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.druid.filters}")
private String filters;
@Value("${spring.datasource.druid.use-global-data-source-stat}")
private Boolean useGlobalDataSourceStat;
@Value("${spring.datasource.druid.connect-properties}")
private Properties connectProperties;
/**
* 数据库参数注入
* @return
* @throws Exception
*/
@Bean
@Primary
public DataSource druidDataSource() throws Exception {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(username);
// 解密后,再 set 进对象
datasource.setPassword(ConfigTools.decrypt(publicKey, password));
logger.info("密码:" + ConfigTools.decrypt(publicKey, password));
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
datasource.setConnectProperties(connectProperties);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("========druid configuration initialization filter========", e);
}
return datasource;
}
/**
* 生成公私钥以及加密密码
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String password = "****";
String[] arr = ConfigTools.genKeyPair(512);
System.out.println("password:" + password);
System.out.println("privateKey:" + arr[0]);
System.out.println("publicKey:" + arr[1]);
System.out.println("password:" + ConfigTools.encrypt(arr[0], password));
}
}