今天小编给大家分享一下SpringBoot怎么进行整合Druid数据源的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。1.自定
今天小编给大家分享一下SpringBoot怎么进行整合Druid数据源的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
使用自定义方式整合Druid
在pom.xml添加相关依赖
<!--数据库相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>Mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency> </dependencies>
application.properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.passWord=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
配置Druid的监控页功能:
Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。官方文档 - 配置StatViewServlet
这个StatViewServlet的用途包括:
Druid内置提供一个StatFilter,用于统计监控信息。官方文档 - 配置_StatFilter
WEBStatFilter用于采集web-jdbc关联监控的数据,如SQL监控、URI监控。官方文档 - 配置WebStatFilter
Druid提供了WallFilter,它是基于SQL语义分析来实现防御SQL注入攻击的。官方文档 - 配置 wallfilter
DataSourceConfig
package com.limi.springboottest2.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;@Configurationpublic class DataSourceConfig { @Bean @ConfigurationProperties("spring.datasource")//复用配置文件的数据源配置, 这样就不用写代码一个一个set了 public DataSource dataSource() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); //加入监控和防火墙功能功能 druidDataSource.setFilters("stat,wall"); return druidDataSource; } @Bean public ServletRegistrationBean statViewServlet(){ StatViewServlet statViewServlet = new StatViewServlet(); ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid @Bean public FilterRegistrationBean webStatFilter(){ WebStatFilter webStatFilter = new WebStatFilter(); FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter); filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.CSS,*.ico,/druid/*"); return filterRegistrationBean; }}
HelloController
package com.limi.springboottest2.controller;import org.springframework.beans.factory.annotation.Autowired;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;@Controllerpublic class HelloController { @Autowired private JdbcTemplate jdbcTemplate; @ResponseBody @GetMapping("/sql") public String test1(){ String sql = "select count(*) from t_user"; Long i = jdbcTemplate.queryForObject(sql, Long.class); return i.toString(); }}
多次点击刷新进行访问
登录druid后台查看监控信息
账号admin, 密码123456
使用场景启动器整合druid, 效果和自定义方式一样, 但更加方便
在pom.xml添加相关依赖
<!--数据库相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.17</version> </dependency>
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>2.7.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.limi</groupId> <artifactId>springboot-test2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-test2</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--数据库相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.17</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <!-- 下面插件作用是工程打包时,不将spring-boot-configuration-processor打进包内,让其只在编码的时候有用 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build></project>
application.properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver#监控SpringBean
spring.datasource.druid.aop-patterns=com.limi.springboottest2.*
# 底层开启功能,stat(sql监控),wall(防火墙)
spring.datasource.druid.filters=stat,wall# 配置监控页功能
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.reset-enable=false# 监控web
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'# 对上面filters里面的stat的详细配置
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.config.drop-table-allow=false
HelloController
package com.limi.springboottest2.controller;import org.springframework.beans.factory.annotation.Autowired;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;@Controllerpublic class HelloController { @Autowired private JdbcTemplate jdbcTemplate; @ResponseBody @GetMapping("/sql") public String test1(){ String sql = "select count(*) from t_user"; Long i = jdbcTemplate.queryForObject(sql, Long.class); return i.toString(); }}
多次点击刷新进行访问
登录druid后台查看监控信息
账号admin, 密码123456
以上就是“SpringBoot怎么进行整合Druid数据源”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网精选频道。
--结束END--
本文标题: SpringBoot怎么进行整合Druid数据源
本文链接: https://lsjlt.com/news/341990.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0