springboot+druid多数据源配置,springboot整合druid+mybatis
目录
1.自定义方式1.添加依赖2.编写配置3.测试2 .启动器方式(推荐)1.添加依赖2.编写配置3.测试德鲁伊特是数据库连接池,它能够提供强大的监控和扩展功能。官方文档
Spring Boot整合第三方技术的两种方式:
自定义找开头的人场景
1.自定义方式
使用自定义方式整合德鲁伊特
1.添加依赖
在pom.xml添加相关依赖
!-数据库相关-依赖关系groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-data-JDBC/artifactId/依赖关系groupId MySQL/groupId artifact id MySQL-connector-Java/artifact id版本5 .1 .6/版本/依赖关系groupIdcom.alibaba/groupId人工德鲁伊/人工版本1 .2 .6/版本/依赖关系/依赖关系
2.编写配置
应用程序。属性
服务器。port=8080 spring。数据来源。URL=JDBC :我的SQL ://localhost :3306/db _ spring测试?使用unicode=true字符编码=utf8 spring。数据来源。用户名=root spring。数据来源。密码=123456春。数据来源。driver-class-name=comMySQL。JDBC。驾驶员
配置德鲁伊特的监控页功能:
德鲁伊特内置提供了一个StatViewServlet用于展示德鲁伊特的统计信息。官方文档-配置StatViewServlet
这个StatViewServlet的用途包括:
提供监控信息展示的超文本标记语言页面提供监控信息的JSON APIDruid内置提供一个静态过滤器,用于统计监控信息。官方文档-配置_统计过滤器
web stat过滤器用于采集web-jdbc关联监控的数据,如结构化查询语言监控、URI监控。官方文档-配置web stat过滤器
德鲁伊特提供了墙壁过滤器,它是基于结构化查询语言语义分析来实现防御结构化查询语言注入攻击的。官方文档-配置壁式过滤器
数据源配置
包com。李米。弹簧靴测试2。配置;导入com。阿里巴巴。德鲁伊。游泳池。德鲁伊数据源;导入com。阿里巴巴。德鲁伊。支持。http。statviewservlet导入com。阿里巴巴。德鲁伊。支持。http。web stat过滤器;导入org。spring框架。靴子。语境。属性。配置属性;导入组织。spring框架。靴子。网络。servlet。filterregistrationbean导入org。spring框架。靴子。网络。servlet。servletregistrationbean导入springframe
work.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; } /** * 配置 druid的监控页功能 * @return */ @Bean public ServletRegistrationBean statViewServlet(){ StatViewServlet statViewServlet = new StatViewServlet(); ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*"); //监控页账号密码: registrationBean.addInitParameter("loginUsername","admin"); registrationBean.addInitParameter("loginPassword","123456"); return registrationBean; } /** * WebStatFilter 用于采集web-jdbc关联监控的数据。 */ @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; }}
3.测试
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
2.starter方式(推荐)
使用场景启动器整合druid, 效果和自定义方式一样, 但更加方便
1.添加依赖
在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>
2.编写配置
application.properties
server.port=8080spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#监控SpringBeanspring.datasource.druid.aop-patterns=com.limi.springboottest2.* # 底层开启功能,stat(sql监控),wall(防火墙)spring.datasource.druid.filters=stat,wall
# 配置监控页功能spring.datasource.druid.stat-view-servlet.enabled=truespring.datasource.druid.stat-view-servlet.login-username=adminspring.datasource.druid.stat-view-servlet.login-password=123456spring.datasource.druid.stat-view-servlet.reset-enable=false
# 监控webspring.datasource.druid.web-stat-filter.enabled=truespring.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=1000spring.datasource.druid.filter.stat.log-slow-sql=truespring.datasource.druid.filter.stat.enabled=truespring.datasource.druid.filter.wall.enabled=truespring.datasource.druid.filter.wall.config.drop-table-allow=false
3.测试
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数据源的文章就介绍到这了,更多相关SpringBoot Druid数据源内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。