springboot(四):springboot使用Druid数据库连接池

1.创建项目的方式和上一节相似

将上一节的代码复制过来
springboot(四):springboot使用Druid数据库连接池

测试一下:
springboot(四):springboot使用Druid数据库连接池

2.配置Druid

配置好数据库连接池的参数

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
    username: root
    password: root
    #使用druid数据库连接池
    type: com.alibaba.druid.pool.DruidDataSource
    #最大活跃数
    maxActive: 20
    #初始化数量
    initialSize: 1
    #最大连接等待超时时间
    maxWait: 60000
    #打开PSCache,并且指定每个连接PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
    #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 1 from dual
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
    filters: stat, wall, log4j

3.配置展示页面

使用druid的servlet管理页面作为网站主页,可以通过这个页面管理druid中的数据库信息。
springboot(四):springboot使用Druid数据库连接池

4.测试

访问页面后出现登陆
springboot(四):springboot使用Druid数据库连接池
程序中硬编码了一个账号:
账号:druid
密码:123456

springboot(四):springboot使用Druid数据库连接池

源码:https://github.com/LUK-qianliu/springboot/