在阅读前这篇博客之前请先异步 玩转springboot2.x整合mybatis因为我们这篇博客是在其基础之上进行讲解的。
Druid是什么?
Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。
Druid 具体配置操作
阿里 Druid为SpringBoot 提供专门的start依赖,Mybatis 使用 Druid 相对比较简单,我们只需要引入 Druid 的starter 依赖并添加相关的一些配置即可。
1 引入Druid 的 starter 依赖
<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
#初始化时建立物理连接的个数
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