Druid(新版starter)在SpringBoot下的使用教程
說明
Druid是Java語言中最好的數(shù)據(jù)庫連接池。Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能。DruidDataSource支持的數(shù)據(jù)庫:
理論上說,支持所有有jdbc驅(qū)動的數(shù)據(jù)庫。最近發(fā)現(xiàn)Druid在springboot框架下有更加好用的Druid Spring Boot Starter,可以省去原本寫Druid的一些配置文件或者@Configuration來配置,直接將配置寫在application.yml里,看起來更簡單一些。
快速開始
版本:最新版druid-spring-boot-starter:1.1.10(也只有這個版本開始才有類似spring.datasource.druid.web-stat-filter這樣的配置),依賴關(guān)系如下

更新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
# 填寫你數(shù)據(jù)庫的url、登錄名、密碼和數(shù)據(jù)庫名
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
# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'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優(yōu)先于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
運(yùn)行結(jié)果
訪問:http://localhost:8080/druid/,登錄名:admin,密碼123456

更多版本查看:http://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter
更多參數(shù)設(shè)置,官方文檔說明:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
關(guān)于Druid的中文說明:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
到此這篇關(guān)于Druid(新版starter)在SpringBoot下的使用的文章就介紹到這了,更多相關(guān)SpringBoot使用Druid內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java環(huán)境變量為什么要配置path和classpath詳細(xì)解答
為何配置path?為何配置classpath?當(dāng)時初學(xué)java時只是關(guān)心如何做而不去關(guān)心這些問題,接下來介紹一下,感興趣的朋友可以參考下哦2013-01-01
關(guān)于File與MultipartFile的用法概述
這篇文章主要介紹了關(guān)于File與MultipartFile的用法概述,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring中數(shù)據(jù)訪問對象Data Access Object的介紹
今天小編就為大家分享一篇關(guān)于Spring中數(shù)據(jù)訪問對象Data Access Object的介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
SpringBoot替換默認(rèn)的tomcat服務(wù)器的方法
Tomcat是Apache基金下的一個輕量級的Servlet容器,支持Servlet和JSP,Tomcat具有Web服務(wù)器特有的功能,在SpringBoot框架中,我們使用最多的是Tomcat,這是SpringBoot默認(rèn)的容器技術(shù),本文給大家介紹了Spring?Boot如何替換默認(rèn)的tomcat服務(wù)器,需要的朋友可以參考下2024-08-08
Spring中Bean創(chuàng)建完后打印語句的兩種方法
這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語句的兩種方法,一個是實(shí)現(xiàn)InitializingBean接口,另一個使用@Bean注解和initMethod屬性,通過代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考閱讀2023-07-07
Spring Boot和Docker實(shí)現(xiàn)微服務(wù)部署的方法
這篇文章主要介紹了Spring Boot和Docker實(shí)現(xiàn)微服務(wù)部署的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

