赞
踩
yml配置:
datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver # mysql连接 本地 url: jdbc:mysql://${MYSQL_HOST:127.0.0.1}:${MYSQL_PORT:3306}/${MYSQL_DATABASE:xxxx}?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&tcpKeepAlive=true&serverTimezone=CTT # mysql账号 username: ${MYSQL_USERNAME:root} # mysql密码 password: ${MYSQL_PASSWORD:1234} # 服务器密码 #druid 数据源专有配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,slf4j maxPoolPreparedStatementPerConnectionSize: 20 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
代码配置:
- package com.yd.mental.hospital.server.config;
-
-
- import com.alibaba.druid.pool.DruidDataSource;
- import com.alibaba.druid.support.http.StatViewServlet;
- import com.alibaba.druid.support.http.WebStatFilter;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.web.servlet.FilterRegistrationBean;
- import org.springframework.boot.web.servlet.ServletRegistrationBean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Primary;
-
- import javax.sql.DataSource;
- import java.sql.SQLException;
-
- /**
- * @creed: 阿里连接池后台 http://localhost:9002/druid/index.html 端口号记得换
- * @date 2020/2/25 10:29
- */
- @Configuration
- public class DruidConfig {
-
- @Value("${spring.datasource.url}")
- private String dbUrl;
- @Value("${spring.datasource.username}")
- private String username;
- @Value("${spring.datasource.password}")
- private String password;
- @Value("${spring.datasource.driver-class-name}")
- private String driverClassName;
- @Value("${spring.datasource.initialSize}")
- private int initialSize;
- @Value("${spring.datasource.minIdle}")
- private int minIdle;
- @Value("${spring.datasource.maxActive}")
- private int maxActive;
- @Value("${spring.datasource.maxWait}")
- private int maxWait;
- @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
- private int timeBetweenEvictionRunsMillis;
- @Value("${spring.datasource.minEvictableIdleTimeMillis}")
- private int minEvictableIdleTimeMillis;
- @Value("${spring.datasource.validationQuery}")
- private String validationQuery;
- @Value("${spring.datasource.testWhileIdle}")
- private boolean testWhileIdle;
- @Value("${spring.datasource.testOnBorrow}")
- private boolean testOnBorrow;
- @Value("${spring.datasource.testOnReturn}")
- private boolean testOnReturn;
- @Value("${spring.datasource.poolPreparedStatements}")
- private boolean poolPreparedStatements;
- @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
- private int maxPoolPreparedStatementPerConnectionSize;
- @Value("${spring.datasource.filters}")
- private String filters;
- @Value("{spring.datasource.connectionProperties}")
- private String connectionProperties;
-
- @Bean //声明其为Bean实例
- @Primary //在同样的DataSource中,首先使用被标注的DataSource
- public DataSource dataSource() {
- DruidDataSource datasource = new DruidDataSource();
- datasource.setUrl(this.dbUrl);
- datasource.setUsername(username);
- datasource.setPassword(password);
- datasource.setDriverClassName(driverClassName);
-
- //configuration
- 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.setPoolPreparedStatements(poolPreparedStatements);
- datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
- try {
- datasource.setFilters(filters);
- } catch (SQLException e) {
- System.err.println("druid configuration initialization filter: " + e);
- }
- datasource.setConnectionProperties(connectionProperties);
- return datasource;
- }
-
- @Bean
- public ServletRegistrationBean statViewServlet() {
- //创建servlet注册实体
- ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
- //设置ip白名单
- servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
- //设置ip黑名单
- servletRegistrationBean.addInitParameter("deny", "192.168.0.2");
- //设置控制台管理用户__登录用户名和密码
- servletRegistrationBean.addInitParameter("loginUsername", "druid");
- servletRegistrationBean.addInitParameter("loginPassword", "123456");
- //是否可以重置数据
- servletRegistrationBean.addInitParameter("resetEnable", "false");
- return servletRegistrationBean;
- }
-
- @Bean
- public FilterRegistrationBean statFilter() {
- //创建过滤器
- FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
- //设置过滤器过滤路径
- filterRegistrationBean.addUrlPatterns("/*");
- //忽略过滤的形式
- filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.png,*.css,*.ico,/druid/*");
- return filterRegistrationBean;
- }
- }
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.11</version> </dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。