SpringBoot(数据库操作 + druid监控功能)

文章目录

    • 1.JDBC + HikariDataSource(SpringBoot2默认数据源)
        • 1.数据库表设计
        • 2.引入依赖 pom.xml
        • 3.配置数据源参数 application.yml
        • 4.编写一个bean,映射表
        • 5.编写测试类来完成测试
          • 1.引入依赖 pom.xml
          • 2.使用JdbcTemplate进行测试
          • 3.成功!
    • 2.整合Druid到SpringBoot
        • 1.基本介绍
        • 2.Druid基本使用
          • 1.引入德鲁伊依赖 pom.xml
          • 2.使用配置类注入DruidDataSource
          • 3.测试
          • 4.细节说明
    • 3.Druid监控功能
        • 1.SQL监控
          • 1.配置类中配置
          • 2.浏览器输入http://localhost:8080/druid/index.html查看监控页面
          • 3.配置类注入数据源时开启sql监控
          • 4.编写Controller进行测试
          • 5.浏览器请求Controller进行测试
          • 6.关于区间分布
        • 2.Web关联监控
          • 1.配置类配置webStatFilter
          • 2.测试
        • 3.SQL防火墙
          • 1.配置类注入数据源时添加过滤器
          • 2.测试
        • 4.Session监控(默认开启)
          • 测试
    • 4.目前阶段完整配置文件
        • 1.pom.xml
        • 2.application.yml 配置数据源信息
        • 3.DruidDataSourceConfig.java 配置类
    • 5.使用starter方式整合druid
        • 1.取消之前对druid的配置
          • 1.修改pom.xml注销druid的依赖
          • 2.注销整个DruidDataSourceConfig.java
          • 3.测试
        • 2.pom.xml 引入依赖
        • 3.application.yml 配置数据源和druid监控

1.JDBC + HikariDataSource(SpringBoot2默认数据源)

1.数据库表设计
-- 创建 spring_boot
DROP DATABASE IF EXISTS spring_boot;
CREATE DATABASE spring_boot;USE spring_boot; -- 创建家居表
CREATE TABLE furn(
`id` INT(11) PRIMARY KEY AUTO_INCREMENT, ## id
`name` VARCHAR(64) NOT NULL, ## 家居名
`maker` VARCHAR(64) NOT NULL, ## 厂商
`price` DECIMAL(11,2) NOT NULL, ## 价格
`sales` INT(11) NOT NULL, ## 销量
`stock` INT(11) NOT NULL, ## 库存
`img_path` VARCHAR(256) NOT NULL ## 照片路径
);-- 初始化家居数据
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`)
VALUES(NULL , ' 北 欧 风 格 小 桌 子 ' , ' 熊 猫 家 居 ' , 180 , 666 , 7 ,
'assets/images/product-image/1.jpg');
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`)
VALUES(NULL , ' 简 约 风 格 小 椅 子 ' , ' 熊 猫 家 居 ' , 180 , 666 , 7 ,
'assets/images/product-image/2.jpg');
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`)
VALUES(NULL , ' 典 雅 风 格 小 台 灯 ' , ' 蚂 蚁 家 居 ' , 180 , 666 , 7 ,
'assets/images/product-image/3.jpg');
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`)
VALUES(NULL , ' 温 馨 风 格 盆 景 架 ' , ' 蚂 蚁 家 居 ' , 180 , 666 , 7 ,
'assets/images/product-image/4.jpg');
2.引入依赖 pom.xml
        <!--引入data-jdbc数据源--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency><!--引入mysql的驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--这里没有使用版本仲裁--><version>5.1.49</version></dependency>
3.配置数据源参数 application.yml
spring:datasource: #配置数据源url: jdbc:mysql://localhost:3306/spring_boot?useSSL=false&useUnicode=true&characterEncoding=UTF-8username: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver
4.编写一个bean,映射表
package com.sun.springboot.bean;import lombok.Data;import java.math.BigDecimal;/*** @author 孙显圣* @version 1.0*/
@Data
public class Furn {private Integer id;private String name;private String maker;private BigDecimal price;private Integer sales;private Integer stock;private String imgPath;
}
5.编写测试类来完成测试
1.引入依赖 pom.xml
        <!--引入starter-test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency>
2.使用JdbcTemplate进行测试
package com.sun.springboot;import com.sun.springboot.bean.Furn;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;import javax.annotation.Resource;
import java.util.List;/*** @author 孙显圣* @version 1.0*/
@SpringBootTest
public class ApplicationTests {//使用JdbcTemplate@Resourceprivate JdbcTemplate jdbcTemplate;@Testpublic void contextLoads() {//使用这个对象来封装查询结果BeanPropertyRowMapper<Furn> furnBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Furn.class);//进行查询List<Furn> query = jdbcTemplate.query("select * from `furn`", furnBeanPropertyRowMapper);for (Furn furn : query) {System.out.println(furn);}}
}
3.成功!

image-20240316173540854

2.整合Druid到SpringBoot

1.基本介绍

image-20240316183227985

2.Druid基本使用
1.引入德鲁伊依赖 pom.xml
        <!--引入druid依赖--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.17</version></dependency>
2.使用配置类注入DruidDataSource
package com.sun.springboot.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;/*** @author 孙显圣* @version 1.0*/
@Configuration
public class DruidDataSourceConfig {//注入一个德鲁伊数据源@ConfigurationProperties("spring.datasource") //读取yaml配置文件的参数,获取数据源配置@Beanpublic DataSource dataSource() {return new DruidDataSource();}
}
3.测试

image-20240316185111707

4.细节说明
  • @ConfigurationProperties(“spring.datasource”)只要配置了这个,就会自动读取yaml配置文件中配置的数据源信息
  • 只要注入了德鲁伊的数据源,则原来默认的数据源将不会被注入因为默认数据源的注入有条件注入的注解,只有在没有配置数据源的时候才会被注入

3.Druid监控功能

1.SQL监控
1.配置类中配置
    //配置德鲁伊监控sql功能@Beanpublic ServletRegistrationBean statViewServlet() {StatViewServlet statViewServlet = new StatViewServlet();ServletRegistrationBean<StatViewServlet> registrationBean =new ServletRegistrationBean<>(statViewServlet, "/druid/*");//配置登录监控页面用户名和密码registrationBean.addInitParameter("loginUsername", "root");registrationBean.addInitParameter("loginPassword", "root");return registrationBean;}
2.浏览器输入http://localhost:8080/druid/index.html查看监控页面

image-20240317095224595

3.配置类注入数据源时开启sql监控

image-20240317095522793

4.编写Controller进行测试
package com.sun.springboot.controller;import com.sun.springboot.bean.Furn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;/*** @author 孙显圣* @version 1.0*/
@Controller
public class DruidSqlController {@AutowiredJdbcTemplate jdbcTemplate;@ResponseBody@GetMapping("/sql")public List<Furn> crudDB(){BeanPropertyRowMapper<Furn> rowMapper =new BeanPropertyRowMapper<>(Furn.class);List<Furn> furns = jdbcTemplate.query("select * from furn", rowMapper);for (Furn furn : furns) {System.out.println(furn);}return furns;}
}
5.浏览器请求Controller进行测试

image-20240317100130781

image-20240317100137652

6.关于区间分布

image-20240317100309519

2.Web关联监控
1.配置类配置webStatFilter
    //配置webStatFilter@Beanpublic FilterRegistrationBean webStatFilter() {WebStatFilter webStatFilter = new WebStatFilter();FilterRegistrationBean<WebStatFilter> filterRegistrationBean =new FilterRegistrationBean<>(webStatFilter);//默认对所有 URL 请求监控filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));//排除 URLfilterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}
2.测试

image-20240317101645787

image-20240317101628965

3.SQL防火墙
1.配置类注入数据源时添加过滤器

image-20240317102059085

2.测试

image-20240317101645787

image-20240317102230202

4.Session监控(默认开启)
测试

image-20240317102507891

image-20240317102516873

4.目前阶段完整配置文件

1.pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>springboot-usersys</artifactId><version>1.0-SNAPSHOT</version><name>Archetype - springboot-usersys</name><url>http://maven.apache.org</url><!--导入springboot父工程--><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.5.3</version></parent><dependencies><!--web场景启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></dependency><!--thymeleaf场景启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--引入data-jdbc数据源--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency><!--引入mysql的驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--这里没有使用版本仲裁--><version>5.1.49</version></dependency><!--引入starter-test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--引入druid依赖--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.17</version></dependency></dependencies>
</project>
2.application.yml 配置数据源信息
spring:datasource: #配置数据源url: jdbc:mysql://localhost:3306/spring_boot?useSSL=false&useUnicode=true&characterEncoding=UTF-8username: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver
3.DruidDataSourceConfig.java 配置类
package com.sun.springboot.config;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.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 javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;/*** @author 孙显圣* @version 1.0*/
@Configuration
public class DruidDataSourceConfig {//注入一个德鲁伊数据源@ConfigurationProperties("spring.datasource") //读取yaml配置文件的参数,获取数据源配置@Beanpublic DataSource dataSource() throws SQLException {DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setFilters("stat, wall"); //开启sql监控return druidDataSource;}//配置德鲁伊监控sql功能@Beanpublic ServletRegistrationBean statViewServlet() {StatViewServlet statViewServlet = new StatViewServlet();ServletRegistrationBean<StatViewServlet> registrationBean =new ServletRegistrationBean<>(statViewServlet, "/druid/*");//配置登录监控页面用户名和密码registrationBean.addInitParameter("loginUsername", "root");registrationBean.addInitParameter("loginPassword", "root");return registrationBean;}//配置webStatFilter@Beanpublic FilterRegistrationBean webStatFilter() {WebStatFilter webStatFilter = new WebStatFilter();FilterRegistrationBean<WebStatFilter> filterRegistrationBean =new FilterRegistrationBean<>(webStatFilter);//默认对所有 URL 请求监控filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));//排除 URLfilterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}
}

5.使用starter方式整合druid

1.取消之前对druid的配置
1.修改pom.xml注销druid的依赖

image-20240317103054268

2.注销整个DruidDataSourceConfig.java
3.测试

image-20240317103302209

2.pom.xml 引入依赖
        <!--引入德鲁伊的starter--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.17</version></dependency>
3.application.yml 配置数据源和druid监控
spring:datasource: #配置数据源url: jdbc:mysql://localhost:3306/spring_boot?useSSL=false&useUnicode=true&characterEncoding=UTF-8username: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver#配置德鲁伊监控功能druid:stat-view-servlet:enabled: truelogin-username: rootlogin-password: rootreset-enable: false#配置web监控web-stat-filter:enabled: trueurl-pattern: /*exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"#配置sql监控filter:stat:log-slow-sql: trueslow-sql-millis: 1000 #1000毫秒就算慢查询enabled: truewall:enabled: trueconfig:#黑白名单drop-table-allow: false #不允许删除表select-all-column-allow: false #不允许查询所有字段

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2870010.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

将OpenCV与gcc和CMake结合使用

返回&#xff1a;OpenCV系列文章目录&#xff08;持续更新中......&#xff09; 上一篇&#xff1a;OpenCV4.9.0开源计算机视觉库在 Linux 中安装 下一篇&#xff1a; 引言&#xff1a; 近年来&#xff0c;计算机视觉技术在图像处理、目标检测和机器人等方面得到了广泛的应用…

YOLOv9改进策略:注意力机制 | 归一化的注意力模块(NAM)

&#x1f4a1;&#x1f4a1;&#x1f4a1;本文改进内容&#xff1a; NAM作为一种高效且轻量级的注意力机制。采用了CBAM的模块集成并重新设计了通道和空间注意子模块。 yolov9-c-NAMAttention summary: 965 layers, 51000614 parameters, 51000582 gradients, 238.9 GFLOPs 改…

服务器机器学习环境搭建(包括AanConda的安装和Pytorch的安装)

服务器机器学习环境搭建 1 服务器与用户 在学校中&#xff0c;我们在学校中是以用户的身份进行访问学校的服务器的。整体框架大致如下&#xff1a; 我们与root用户共享服务器的一些资源&#xff0c;比如显卡驱动&#xff0c;Cuda以及一些其他的公共软件。 一般情况下&#…

迷茫了!去大厂还是创业?

大家好&#xff0c;我是麦叔&#xff0c;最近我创建了一个 学习圈子 有球友在 星球 里提问。 大厂的layout岗位和小厂的硬件工程师岗位&#xff0c;该如何选择&#xff1f; 这个问题我曾经也纠结过&#xff0c;不过现在的我&#xff0c;I am awake&#xff01; 肯定是有大点大。…

【Java基础知识总结 | 第二篇】深入理解分析ArrayList源码

文章目录 3.深入理解分析ArrayList源码3.1ArrayList简介3.2ArrayLisy和Vector的区别&#xff1f;3.3ArrayList核心源码解读3.3.1ArrayList存储机制&#xff08;1&#xff09;构造函数&#xff08;2&#xff09;add()方法&#xff08;3&#xff09;新增元素大体流程 3.3.2ArrayL…

探索设计模式的魅力:探索发布-订阅模式的深度奥秘-实现高效、解耦的系统通信

​&#x1f308; 个人主页&#xff1a;danci_ &#x1f525; 系列专栏&#xff1a;《设计模式》 &#x1f4aa;&#x1f3fb; 制定明确可量化的目标&#xff0c;并坚持默默的做事。 探索发布-订阅模式的深度奥秘&#xff1a;实现高效、解耦的系统通信 文章目录 一、案例场景&am…

【四 (5)数据可视化之 Pyecharts常用图表及代码实现 】

目录 文章导航一、介绍[✨ 特性]二、安装Pyecharts三、主题风格四、占比类图表1、饼图2、环形图3、玫瑰图4、玫瑰图-多图5、堆叠条形图6、百分比堆叠条形图 五、比较排序类1、条形图2、雷达图3、词云图4、漏斗图 六、趋势类图表1、折线图2、堆叠折线图3、面积图4、堆叠面积图 七…

创建硬件企业的8个要求

目录 内容简介 1. 长期愿景和目标 2. 适应和学习能力 3. 能够理解技术方面的信息 4. 建立关系的能力 5. 现金流 6. 可用时间和资金平衡 7. 一次专注于一种产品 8. 实现长期成功的耐心 CSDN学院 专栏作家 内容简介 为了创建成功的硬件产品&#xff0c;你需要具备各种…

如何在Windows系统搭建Emby影音平台并实现远程访问本地文件【内网穿透】

文章目录 1.前言2. Emby网站搭建2.1. Emby下载和安装2.2 Emby网页测试 3. 本地网页发布3.1 注册并安装cpolar内网穿透3.2 Cpolar云端设置3.3 Cpolar内网穿透本地设置 4.公网访问测试5.结语 1.前言 在现代五花八门的网络应用场景中&#xff0c;观看视频绝对是主力应用场景之一&…

Linux系统安全②SNAT与DNAT

目录 一.SNAT 1.定义 2.实验环境准备 &#xff08;1&#xff09;三台服务器&#xff1a;PC1客户端、PC2网关、PC3服务端。 &#xff08;2&#xff09;硬件要求&#xff1a;PC1和PC3均只需一块网卡、PC2需要2块网卡 &#xff08;3&#xff09;网络模式要求&#xff1a;PC1…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的自动驾驶目标检测系统详解(深度学习+Python代码+PySide6界面+训练数据集)

摘要&#xff1a;开发自动驾驶目标检测系统对于提高车辆的安全性和智能化水平具有至关重要的作用。本篇博客详细介绍了如何运用深度学习构建一个自动驾驶目标检测系统&#xff0c;并提供了完整的实现代码。该系统基于强大的YOLOv8算法&#xff0c;并对比了YOLOv7、YOLOv6、YOLO…

IntelliJ IDEA 2023.3.4创建JavaWeb应用和集成Tomcat服务器

1. 创建项目 如下图所示&#xff0c;只需要给项目起一个项目名称&#xff0c;然后点击Create即可&#xff1a; 2. Project Structure 设置 创建完成后如下图 3. 集成Tomcat服务器 4. 实现Servlet接口 当我们实现Servlet接口时&#xff0c;发现没有Servlet相关的依赖时&am…

AcWing 2. 01背包问题

题目描述 解题思路&#xff1a; 相关代码&#xff1a; import java.util.Scanner; public class Main {public static void main(String[] args){Scanner scanner new Scanner(System.in);/** 背包问题的物品下标最好从1开始。* *//*定义一f[i][j]数组&#xff0c;i表示的…

PDF Expert:强大注释与批注功能,让PDF阅读更高效

PDF Expert软件是一款功能丰富且强大的PDF编辑和管理工具&#xff0c;为用户提供了全面的PDF处理解决方案。以下是其主要的功能特色介绍&#xff1a; PDF编辑功能&#xff1a;PDF Expert允许用户对PDF文件进行深度编辑。这包括但不限于添加、删除、重新排列和合并页面&#xff…

SQLiteC/C++接口详细介绍之sqlite3类(十四)

返回目录&#xff1a;SQLite—免费开源数据库系列文章目录 上一篇&#xff1a;SQLiteC/C接口详细介绍之sqlite3类&#xff08;十三&#xff09; 下一篇&#xff1a;SQLiteC/C接口详细介绍之sqlite3类&#xff08;十五&#xff09; 43.sqlite3_preupdate_hook sqlite3_preup…

Camtasia 2023 中文MacOS

Camtasia 2023软件在录屏软件中的确表现突出&#xff0c;可以说是佼佼者之一。这款软件不仅功能强大&#xff0c;而且操作简便&#xff0c;适用于各种屏幕录制和视频编辑需求。 一、屏幕录制与视频导入 Camtasia 2023提供了高清的屏幕录制功能&#xff0c;可以轻松地捕捉电脑…

SpringCloud-深度理解ElasticSearch

一、Elasticsearch概述 1、Elasticsearch介绍 Elasticsearch&#xff08;简称ES&#xff09;是一个开源的分布式搜索和分析引擎&#xff0c;构建在Apache Lucene基础上。它提供了一个强大而灵活的工具&#xff0c;用于全文搜索、结构化搜索、分析以及数据可视化。ES最初设计用…

应用程序开发教学:医保购药系统源码搭建实战

医保购药系统作为医疗服务的重要组成部分&#xff0c;其开发不仅能够为患者提供更加便捷的购药服务&#xff0c;还能够提高医疗机构的管理效率。接下来&#xff0c;小编将为您讲解医保购药系统的源码搭建过程&#xff0c;介绍应用程序开发的基本步骤和技巧。 一、系统设计 我…

矩阵中移动的最大次数

文章目录 所属专栏:BFS算法 题目链接 思路如下&#xff1a; 1.首先我们需要从第一列开始遍历&#xff0c;寻找每一个都能够满足条件的位置&#xff0c;将它插入到数组里面 2.第一列遍历完了后我们先判断第一列的数是否都满足条件插入到数组里面&#xff0c;如果数组为空&#…

关于微信公众号的一些个心得(持续更新)

微信公众号也是写一些个人心得&#xff0c;也不指望有人关注什么的&#xff0c;如果在一个领域可以深耕的话也希望可以做一些分享。目前也就是写一些心得和体验&#xff0c;摘抄一类的。 字体大小和排版什么的有没有人有经验啊 安装编辑插件&#xff0c;以chorme浏览器为例&a…