点击查看数据访问demo:LearnSpringBoot06DataJdbc
点击查看更多的SpringBoot教程
简介
Druid Spring Boot Starter 用于帮助你在Spring Boot项目中轻松集成Druid数据库连接池和监控。
一、添加druid-spring-boot-starter依赖
点击查询最新版
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.18</version>
</dependency>
二、配置属性
application.yml代码
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.0.102:3307/dbjdbc
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j2
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# mybatis
mybatis:
# 指定全局配置文件位置
config-location: classpath:mybatis/mybatis-config.xml
# 指定sql映射文件位置
mapper-locations: classpath:mybatis/mapper/*.xml
# 表创建好了,将下面代码注释掉
# sql:
# init:
# username: root
# password: 123456
# mode: always
# schema-locations:
# - classpath*:sql/schema.sql
# data-locations:
# - classpath*:sql/schema-data.sql
# Spring Boot 之五:Spring 属性配置 https://www.jianshu.com/p/cfeae8c1b8dd
# Springboot 2.5.x初始化执行sql文件配置 https://blog.csdn.net/qq_24258617/article/details/120272350
# 查看源码 DataSourceProperties DataSourceConfiguration
DruidConfig.java代码
package com.example.learnspringboot06datajdbc.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DruidDataSource druidDataSource(){
return new DruidDataSource();
}
//配置Druid的监控
//1、配置一个管理后台的Servlet
// 运行起来之后 打开 http://localhost:8082/druid/login.html
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//默认就是允许所有访问
initParams.put("deny","192.168.0.104");//设置黑名单
bean.setInitParameters(initParams);
return bean;
}
//2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
pom.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>LearnSpringBoot06DataJdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LearnSpringBoot06DataJdbc</name>
<description>LearnSpringBoot06DataJdbc</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<!--
【问题解决】Springboot3.0.3+找不到MySQL驱动
https://blog.csdn.net/qq_39320261/article/details/129394183
https://blog.csdn.net/ZHENFENGSHISAN/article/details/130992615
-->
<scope>runtime</scope>
</dependency>
<!-- 引入自定义的数据源 https://mvnrepository.com/artifact/com.alibaba/druid -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>1.1.22</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.18</version>
</dependency>
<!--
SpringBoot 整合 durid数据库连接池
https://blog.csdn.net/qq_45315910/article/details/94736535
这里 注释掉,不用再添加依赖了,查看pom文件的依赖关系图,已经依赖 slf4j 了
-->
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-log4j12</artifactId>-->
<!-- </dependency>-->
<!--使用 log4j2 记录日志
原文链接:https://blog.csdn.net/m0_71777195/article/details/126053802
这里 注释掉,不用再添加依赖了,查看pom文件的依赖关系图,已经依赖 slf4j 了
-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-log4j2</artifactId>-->
<!-- </dependency>-->
<!-- https://blog.csdn.net/weixin_37799575/article/details/125102566 -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid-spring-boot-starter</artifactId>-->
<!-- <version>1.1.22</version>-->
<!-- </dependency>-->
<!-- mybatis https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、工程结构图
工程:LearnSpringBoot06DataJdbc
数据库
四、DruidMonitor页面介绍
项目运行成功后,在浏览器里访问http://localhost:8082/druid/login.html
用户名:admin,密码:123456
首页
数据源页面
SQL监控页面
SQL防火墙页面
Web应用页面
URL监控页面
Session监控页面
Spring监控页面
五、测试结果
在浏览器访问http://localhost:8082/queryDepartment,即查询所有部门
所有部门数据已查询完毕,来到DruidMonitor 点击URL监控页面
从上图可以看出相关数据已经发生变化了,例如请求次数、jdbc执行数都增加了
查看SQL监控页面数据
从上图可以看出相关数据已经发生变化了,例如执行数都增加了
点击查看数据访问demo:LearnSpringBoot06DataJdbc
**提醒: **
LearnSpringBoot06DataJdbc工程里有部分是整合mybatis相关操作实践的,可以正常使用。