【sping-boot】配置druid 连接池
添加配置
修改pom.xml文件
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency>
修改application.yml文件
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/komo?characterEncoding=utf-8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # Druid连接池配置 druid: # 初始化大小,最小,最大 initial-size: 5 max-active: 30 min-idle: 5 # 获取数据库连接等待的超时时间 max-wait: 3000 max-open-prepared-statements: -1 # 配置多久进行一次检测,检测需要关闭的空闲连接 单位毫秒 time-between-eviction-runs-millis: 60000 # 配置连接在池中的最小生存时间 min-evictable-idle-time-millis: 300000 # 配置连接在池中的最大生存时间 max-evictable-idle-time-millis: 400000 # 系统启动时通过该sql语句验证数据库是否可用,如果不配置validationQuery,则下面三项无效 validation-query: SELECT 1 # 启用空闲连接检测,以便回收 test-while-idle: true # 从连接池获取连接时,是否检测连接可用性,开启性能会有些许影响 test-on-borrow: false # 释放连接到连接池时,是否检测连接可用性,开启性能会有些许影响 test-on-return: false # 打开PSCache,并且指定每个连接上PSCache的大小 pool-prepared-statements: false max-pool-prepared-statement-per-connection-size: 20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall use-global-data-source-stat: true # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 server: port: 7080
添加配置druid配置
DruidConfig.java
package com.example.druiddemo.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import javax.sql.DataSource; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author komiles@163.com * @date 2020-05-11 16:12 */ @Configuration public class DruidConfig { @Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // IP白名单 servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // IP黑名单(共同存在时,deny优先于allow) // servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); //控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "admin"); //是否能够重置数据 禁用HTML页面上的“Reset All”功能 servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druid(){ return new DruidDataSource(); } }
访问地址
http://localhost:7080/druid/index.html
源码地址
https://github.com/KoMiles/spring-example/tree/master/druid-demo
千里之行,始于足下。改变现在,就是改变未来。改变未来,从现在开始。