-
Notifications
You must be signed in to change notification settings - Fork 130
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是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。
阿里 Druid为SpringBoot 提供专门的start依赖,mybaties 使用druid 相对比较简单,我们只需要引入 druid的start依赖并添加相关的一些配置即可。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
在没有使用 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
添加获取商品信息 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