玩转springboot2.x 通过druid-spring-boot-starter整合Druid(mybatis版)

版权声明:本文为博主原创文章,转载请表明出处。如果您觉得文章还行就点个赞,同时也可以关注一下我哈。 https://blog.csdn.net/ljk126wy/article/details/87895658

在阅读前这篇博客之前请先异步 玩转springboot2.x整合mybatis因为我们这篇博客是在其基础之上进行讲解的。

Druid是什么?

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

Druid 具体配置操作

阿里 Druid为SpringBoot 提供专门的start依赖,mybaties 使用druid 相对比较简单,我们只需要引入 druid的start依赖并添加相关的一些配置即可。

1 引入druid 的start 依赖

		<dependency>
		   <groupId>com.alibaba</groupId>
		   <artifactId>druid-spring-boot-starter</artifactId>
		   <version>1.1.10</version>
		</dependency>

2 添加druid 相关配置

在没有使用 druid 之前的配置如下:

spring.datasource.url=jdbc:mysql://localhost:3306/learn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

使用druid的配置如下:
以下是JDBC 配置必选配置:

spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/learn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

DruidDataSource配置属性列表

#初始化时建立物理连接的个数
spring.datasource.druid.initial-size=3
#最小连接池数量
spring.datasource.druid.min-idle=3
#最大连接池数量
spring.datasource.druid.max-active=10
#获取连接时最大等待时间
spring.datasource.druid.max-wait=60000
#配置监控页面访问登录名称
spring.datasource.druid.stat-view-servlet.login-username=admin
#配置监控页面访问密码
spring.datasource.druid.stat-view-servlet.login-password=admin
#是否开启慢sql查询监控
spring.datasource.druid.filter.stat.log-slow-sql=true
#慢SQL执行时间
spring.datasource.druid.filter.stat.slow-sql-millis=1

测试慢sql 监控

添加获取商品信息 Controller

@RestController
public class ProductController {
	@Autowired
	private ProductMapper productMapper;
	@GetMapping("/productList")
	public Product findById () {
		Product findById = productMapper.findById(1l);
		return findById;
	}
}

在这里插入图片描述
http://localhost:8080/sbe/druid/index.html

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ljk126wy/article/details/87895658