Skip to content

SpringBoot2.0 通过 druid spring boot starter 整合 Druid(mybatis版)

zhuoqianmingyue edited this page Jun 11, 2019 · 2 revisions

查看示例程序说明

在阅读前这篇博客之前请先前往 SpringBoot2.0-整合-MyBatis因为该教程是在SpringBoot2.0-整合-MyBatis基础之上进行讲解的。

SpringBoot2.0 通过 druid-spring-boot-starter 整合 Druid(mybatis版) 教程 在master分支上查看 master mybaties

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

在这里插入图片描述

在这里插入图片描述