很多程序員數(shù)據(jù)庫連接池的印象可有可無,不使用連接池我們的程序也能正常運行,可以無連接池這個概念,但是實際上現(xiàn)在的項目都會使用到阿里巴巴的Druid數(shù)據(jù)源.來看看那Druid入門使用.
1. 在pom文件引入starter
<!--druid數(shù)據(jù)源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
2. 編寫配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
druid:
initialSize: 1
minIdle: 1
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: false
maxOpenPreparedStatements: 20
#開啟StatFilter
filter:
stat:
enabled: true
log-slow-sql: true
slow-sql-millis: 1000
#開啟Slf4jFilter
slf4j:
enabled: true
data-source-log-enabled: false
connection-log-enabled: false
statement-log-enabled: false
result-set-log-enabled: false
#開啟WallFilter
wall:
enabled: true
log-violation: true
throw-exception: false
config:
delete-where-none-check: true
#開啟Web監(jiān)控
web-stat-filter:
enabled: true
exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
url-pattern: /*
#開啟監(jiān)控頁面
stat-view-servlet:
enabled: true
login-username: admin
login-password: z1320291471
3. 這里修改配置文件即刻,連接池就配置完成了 當然這里配置的監(jiān)控頁面,可以輸入 127.0.0.1:8080/druid
,查看監(jiān)控頁面.登錄賬號密碼就是上方配置的賬號密碼login-username: admin
,login-password: z1320291471
整個頁面可以看到的東西還是蠻多.以下是部分截圖.