springboot集成druid连接池
使用druid连接池主要有几步:
1、添加jar和依赖
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
2、配置文件:
server:
port: 1111
spring:
application:
name: springboothouse
druid:
druidClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/houseselling?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: *****
#最大连接数
maxActive: 30
#最小连接数
minIdle: 5
#获取连接的最大等待时间
maxWait: 10000
#解决mysql8小时的问题
validationgQuery: SELECT 'X'
#空闲连接的检查时间间隔
timeBetweenEvictionRunsMillis: 60000
#空闲连接最小空闲时间
minEvictableIdleTimeMillis: 300000
3、配置连接池的监控和慢sql处理
连接池的监控连接:
http://ip:port/ 项目名/druid/或
http://ip:port/ 项目名/druid/index.html即可访问

package com.house.sell.config; import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.google.common.collect.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /**
*druid连接池的配置,配置如何处理慢sql,
*/
@Configuration
public class DruidConfig {
//这个注解读取配置文件前缀为prefix的配置,将外部的配置文件与这里绑定
@ConfigurationProperties(prefix = "spring.druid")
//容器的开启与关闭
@Bean(initMethod = "init",destroyMethod = "close")
public DruidDataSource dataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
return dataSource;
}
//bean注解,成为spring的bean,利用filter将慢sql的日志打印出来
@Bean
public Filter statFilter(){
StatFilter statFilter=new StatFilter();
//多长时间定义为慢sql,这里定义为5s
statFilter.setSlowSqlMillis(5000);
//是否打印出慢日志
statFilter.setLogSlowSql(true);
//是否将日志合并起来
statFilter.setMergeSql(true);
return statFilter;
}
//这是配置druid的监控
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
} }
springboot集成druid连接池的更多相关文章
- SpringBoot集成druid数据库连接池的简单使用
简介 Druid是阿里巴巴旗下Java语言中最好的数据库连接池.Druid能够提供强大的监控和扩展功能. 官网: https://github.com/alibaba/druid/wiki/常见问题 ...
- springboot整合druid连接池、mybatis实现多数据源动态切换
demo环境: JDK 1.8 ,Spring boot 1.5.14 一 整合durid 1.添加druid连接池maven依赖 <dependency> <groupId> ...
- SpringBoot2.0 基础案例(07):集成Druid连接池,配置监控界面
一.Druid连接池 1.druid简介 Druid连接池是阿里巴巴开源的数据库连接池项目.Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能.功能强大,能防SQL注入,内置Login ...
- springboot使用druid连接池连接Oracle数据库的基本配置
#阿里连接池配置 #spring.datasource.druid.driver-class-name=oracle.jdbc.driver.OracleDriver #可配可不配,阿里的数据库连接池 ...
- SpringBoot下Druid连接池的使用配置
Druid是一个JDBC组件,druid 是阿里开源在 github 上面的数据库连接池,它包括三部分: * DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体 ...
- Spring系列之集成Druid连接池及监控配置
前言 前一篇文章我们熟悉了HikariCP连接池,也了解到它的性能很高,今天我们讲一下另一款比较受欢迎的连接池:Druid,这是阿里开源的一款数据库连接池,它官网上声称:为监控而生!他可以实现页面监控 ...
- SpringBoot 使用Druid连接池
1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- springboot 学习之路 6(集成durid连接池)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- spring boot配置druid连接池连接mysql
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
随机推荐
- jeecg的下拉列表
jeecg里面下拉列表的使用 ①建立数据字典seo_id <t:dictSelect field="operationPromotionAccount" typeGroupC ...
- 个人项目junit4测试
一.题目简介 用java编写一个程序,模拟ATM柜员机. 二.源码的github链接 www.github.com/liuxianchen/test 三.所设计的模块测试用例.测试结果截图 四 心得 ...
- APP推广(预期方案)
首先,在推广过程中有一些定的弊端:我们这个O2O平台暂时只能适用于学校局域网. 因为我们的APP才刚刚“出炉”不久,在网络上还是属于一篇空白的状态,我们想过可以在百度百科上进行相应的推广,如果有用户搜 ...
- [转帖]紫光展锐5G芯片
紫光展锐5G芯片已流片:7nm工艺 2019年问世 https://news.mydrivers.com/1/612/612346.htm 本文转载自超能网,其他媒体转载需经超能网同意 高通骁龙X ...
- 转帖 Oracle 主键的处理方法 http://www.cnblogs.com/Richardzhu/p/3470929.html
Oracle之主键的创建.添加.删除操作 一.创建表的同时创建主键约束 1.1.无命名 SQL> create table jack (id int primary key not null ...
- Jquery 组 表单验证
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- node 和npm 版本更新
node 版本更新 由于公司要用NG-ZORRO,于是我就跑到官网先看看demo,怎么构建项目,执行的过程中发现了问题 问题描述 执行官网构建项目命令 安装脚手架工具# $ npm install - ...
- Java代码redis基础操作
maven依赖包: <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
- CSS兼容性详解
前面的话 对于前端工程师来说,不想面对又不得不面对的一个问题就是兼容性.在几年之前,处理兼容性,一般地就是处理IE低版本浏览器的兼容性.而近几年,随着移动端的发展,工程师也需要注意手机兼容性了.本文将 ...
- 改变自己从学习linux开始
刚刚高中毕业,进如大学的时候,总以为摆脱了束缚可以无拘无束的玩耍了.当时真的就是和众多大学生一起,像撒欢的野马,每天逃课,上网,泡吧,不把学习当一会事,学校里教授讲的各种知识也没有听在心里,前两年玩的 ...