spring boot版本:2.3.12.RELEASE
MySQL版本:8.0
Druid简介
Druid是阿里开源的一个数据库连接池和SQL查询优化工具,用于提高应用程序对数据库的性能和可扩展性。主要提供的功能:数据库连接池、数据库连接池监控、SQL查询优化、数据源管理、防御SQL注入、统计和监控。
引入pom
依赖
<!--druid-spring-boot-starter-->
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.16</version>
</dependency><!-- mysql-connector-j -->
<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.33</version>
</dependency>
application.yml
配置
server:port: 8989servlet:context-path: /druidDemospring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/spring_boot_test?allowMultiQueries=true&serverTimezone=Asia/Shanghai&useSSL=falseusername: rootpassword: 123456# 如果initialSize数量较多时,打开会加快应用启动时间async-init: true# 连接池初始化时创建的连接数initial-size: 5# 连接池中最大连接数max-active: 50# 连接池中最小空闲连接数min-idle: 5# 连接池中最大空闲连接数max-idle: 20# 配置获取连接等待超时的时间 毫秒max-wait: 6000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis: 60000# 增强timeBetweenEvictionRunsMillis的周期性连接检查,minIdle内的空闲连接,每次检查强制验证连接有效性keep-alive: true# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis: 300000max-evictable-idle-time-millis: 172800000# 用于检测连接是否有效的SQL语句validation-query: select 1validation-query-timeout: 1# 是否开启空闲连接的检测test-while-idle: true# 是否开启连接的检测功能,在获取连接时检测连接是否有效test-on-borrow: false# 是否开启连接的检测功能,在归还连接时检测连接是否有效test-on-return: false# 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。pool-prepared-statements: false# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙# 配置监拉统计挡成的filters. stat: 监控统计、Log4j:日志记录、waLL: 防御sqL注入filters: stat,wall,log4j2# 配置后台监控stat-view-servlet:# http://localhost:8989/mp/druid/index.html# 启用StatViewServletenabled: true# 访问内置监控页面的路径,内置监控页面的首页是/druid/index.htmlurl-pattern: /druid/*# 禁用页面重置按钮reset-enable: true# 配置监控页面访问密码login-username: rootlogin-password: 123456# IP 黑名单,deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝deny:# IP 白名单,没有配置或者为空则允许所有访问allow: 127.0.0.1# 配置过滤器,过滤掉静态文件web-stat-filter:enabled: trueurl-pattern: /*exclusions: /druid/*,*.js,*.css,*.gif,*.jpg,*.bmp,*.png,*.icomybatis-plus:# 实体类Mapper.xml文件所在包mapper-locations: classpath:mapper/*Mapper.xml# 指定 MyBatis 别名包扫描路径,用于给包中的类注册别名。注册后,在 Mapper 对应的 XML 文件中可以直接使用类名,无需使用全限定类名。type-aliases-package: com.example.pojoconfiguration:# 开启驼峰映射,A_COLUMN -> aColumnmap-underscore-to-camel-case: true# 是否允许单条sql 返回多个数据集multiple-result-sets-enabled: true# mybatis-plus的日志输出到控制台log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
访问监控页面
端口是你自己定义的端口,druidDemo
来自于server.servlet.context-path
配置的值,按需修改即可。
浏览器输入:http://127.0.0.1:8989/druidDemo/druid/login.html
,输入你自己配置的账户密码即可访问。