赞
踩
Druid是阿里巴巴开源的一款非常优秀的数据库连接池。在Java应用程序开发中,常用的连接池还有DBCP、C3P0、Proxool等。
SpringBoot2.X 版本开始默认的是HikariCP(号称性能最好的数据库连接池),Druid性能好而且监控也比较方便。
感兴趣的同学可以读一下:Druid连接池介绍
接下来我们就来讲解如何集成Druid数据源
<!--阿里数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- Mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis框架 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.boot.version}</version> </dependency>

spring: ## 数据库配置 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver druid: url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: root # 初始连接数 initialSize: 5 # 最小连接池数量 minIdle: 10 # 最大连接池数量 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最大生存的时间,单位是毫秒 maxEvictableIdleTimeMillis: 900000 # 配置检测连接是否有效 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false webStatFilter: enabled: true statViewServlet: enabled: true # 设置白名单,不填则允许所有访问 allow: url-pattern: /druid/* # 控制台管理用户名和密码 login-username: root login-password: root filter: stat: enabled: true # 慢SQL记录 log-slow-sql: true slow-sql-millis: 1000 merge-sql: true wall: config: multi-statement-allow: true
参数说明:
这样就可以最简单的启动了

有人可能会遇到如下的错误信息:

缺少了类似mybatis、spring-data-jpa这类的数据包,Druid需要依赖这些才能启动
在浏览器输入:http://localhost:8080/druid/login.html,出现Druid监控登录框,用户名和密码为配置文件里的内容


在这里可以查看各类数据的监控信息,帮助系统的优化。
这里只是做了最简单的集成,在后续的项目中使用的话会另起一篇深入探讨。
https://gitee.com/kaixinshow/springboot-note
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。