软件编程
位置:首页>> 软件编程>> java编程>> Druid(新版starter)在SpringBoot下的使用教程

Druid(新版starter)在SpringBoot下的使用教程

作者:芸灵fly  发布时间:2021-07-03 20:25:18 

标签:SpringBoot,Druid

说明

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。DruidDataSource支持的数据库:
理论上说,支持所有有jdbc驱动的数据库。最近发现Druid在springboot框架下有更加好用的Druid Spring Boot Starter,可以省去原本写Druid的一些配置文件或者@Configuration来配置,直接将配置写在application.yml里,看起来更简单一些。

快速开始

版本:最新版druid-spring-boot-starter:1.1.10(也只有这个版本开始才有类似spring.datasource.druid.web-stat-filter这样的配置),依赖关系如下

Druid(新版starter)在SpringBoot下的使用教程

更新pom.xml

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid-spring-boot-starter</artifactId>
  <version>1.1.10</version>
</dependency>

编写application.yml,部分说明写在注释了:

spring:
 application:
   name: springboot-test-exam1
 datasource:
   # 使用阿里的Druid连接池
   type: com.alibaba.druid.pool.DruidDataSource
   driver-class-name: com.mysql.jdbc.Driver
   # 填写你数据库的url、登录名、密码和数据库名
   url: jdbc:mysql://localhost:3306/databaseName?useSSL=false&characterEncoding=utf8
   username: root
   password: root
   druid:
     # 连接池的配置信息
     # 初始化大小,最小,最大
     initial-size: 5
     min-idle: 5
     maxActive: 20
     # 配置获取连接等待超时的时间
     maxWait: 60000
     # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
     timeBetweenEvictionRunsMillis: 60000
     # 配置一个连接在池中最小生存的时间,单位是毫秒
     minEvictableIdleTimeMillis: 300000
     validationQuery: SELECT 1
     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
     # 配置DruidStatFilter
     web-stat-filter:
       enabled: true
       url-pattern: "/*"
       exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
     # 配置DruidStatViewServlet
     stat-view-servlet:
       url-pattern: "/druid/*"
       # IP白名单(没有配置或者为空,则允许所有访问)
       allow: 127.0.0.1,192.168.163.1
       # IP黑名单 (存在共同时,deny优先于allow)
       deny: 192.168.1.73
       #  禁用HTML页面上的“Reset All”功能
       reset-enable: false
       # 登录名
       login-username: admin
       # 登录密码
       login-password: 123456

为了方便使用application.properties的读者,使用下面的配置和上面相同

server.port=8080
spring.application.name=springboot-test-exam1
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/databaseName?useSSL=false&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,wall,slf4j
spring.datasource.druid.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,192.168.163.1
spring.datasource.druid.stat-view-servlet.deny=192.168.1.73
spring.datasource.druid.stat-view-servlet.reset-enable=false
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456

运行结果

访问:http://localhost:8080/druid/,登录名:admin,密码123456

Druid(新版starter)在SpringBoot下的使用教程

更多版本查看:http://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter

更多参数设置,官方文档说明:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

关于Druid的中文说明:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

来源:https://blog.csdn.net/weixin_38187317/article/details/81562571

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com