很多程序员数据库连接池的印象可有可无,不使用连接池我们的程序也能正常运行,可以无连接池这个概念,但是实际上现在的项目都会使用到阿里巴巴的Druid数据源.来看看那Druid入门使用.
1. 在pom文件引入starter
<!--druid数据源-->
<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监控
web-stat-filter:
enabled: true
exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
url-pattern: /*
#开启监控页面
stat-view-servlet:
enabled: true
login-username: admin
login-password: z1320291471
3. 这里修改配置文件即刻,连接池就配置完成了 当然这里配置的监控页面,可以输入 127.0.0.1:8080/druid
,查看监控页面.登录账号密码就是上方配置的账号密码login-username: admin
,login-password: z1320291471
整个页面可以看到的东西还是蛮多.以下是部分截图.