SpringBoot配置MySql数据库和Druid连接池
1.pom文件增加相关依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
2.mysql大家应该都很了解了,这里主要介绍一下druid连接池。之前的项目大多数都使用c3p0或者其他的连接池,这里使用druid连接池,好处就是可以监控数据库访问性能,druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。这里看一下配置文件下配置,这里使用的配置文件是application.yml
spring: #表示该配置直接为Spring容器负责处理
datasource:
type: com.alibaba.druid.pool.DruidDataSource #配置当前要使用的数据源的操作类型那个
driver-class-name: org.gjt.mm.mysql.Driver #配置MySQL的驱动程序类
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
filters: stat,wall,log4j #druid监控配置
dbcp2:
min-idle: 5 #数据库连接池的最小维持连接数
initial-size: 5 #初始化提供的连接数
max-total: 5 #最大的连接数
max-wait-millis: 200 #等待连接获取的最大超时时间
3.最后,还需要增加一个druid的基本配置,这里直接上代码。
import javax.sql.DataSource; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter; @Configuration
public class DruidConfig {
@Bean
public ServletRegistrationBean druidServlet() {// 主要实现web监控的配置处理
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new StatViewServlet(), "/druid/*");//表示进行druid监控的配置处理操作
servletRegistrationBean.addInitParameter("allow", "127.0.0.1,129.168.1.11");//白名单
servletRegistrationBean.addInitParameter("deny", "129.168.1.12");//黑名单
servletRegistrationBean.addInitParameter("loginUsername", "root");//用户名
servletRegistrationBean.addInitParameter("loginPassword", "root");//密码
servletRegistrationBean.addInitParameter("resetEnable", "false");//是否可以重置数据源
return servletRegistrationBean; }
@Bean //监控
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");//所有请求进行监控处理
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");//排除
return filterRegistrationBean;
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
}
我们可以看到这里的几个基本配置,白名单,黑名单,顾名思义就是设置谁可以访问,谁不能访问。用户名, 密码就不用多说了。最后我们登录druid监控,看一下,访问地址:http://localhost:port/druid/login.html,会看到如下界面

使用上面配置好的用户名密码进行登录,便实现了druid监控

SpringBoot配置MySql数据库和Druid连接池的更多相关文章
- spring+mybatis+c3p0数据库连接池或druid连接池使用配置整理
在系统性能优化的时候,或者说在进行代码开发的时候,多数人应该都知道一个很基本的原则,那就是保证功能正常良好的情况下,要尽量减少对数据库的操作. 据我所知,原因大概有这样两个: 一个是,一般情况下系统服 ...
- spring boot配置druid连接池连接mysql
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring配置druid连接池和监控数据库访问性能
Druid连接池及监控在spring配置如下: <bean id="dataSource" class="com.alibaba.druid.pool.DruidD ...
- SpringBoot框架:通过AOP和自定义注解完成druid连接池的动态数据源切换(三)
一.引入依赖 引入数据库连接池的依赖--druid和面向切面编程的依赖--aop,如下所示: <!-- druid --> <dependency> <groupId&g ...
- 六:SpringBoot-集成Druid连接池,配置监控界面
SpringBoot-集成Druid连接池,配置监控界面 1.Druid连接池 1.1 Druid特点 2.SpringBoot整合Druid 2.1 引入核心依赖 2.2 数据源配置文件 2.3 核 ...
- springboot整合druid连接池、mybatis实现多数据源动态切换
demo环境: JDK 1.8 ,Spring boot 1.5.14 一 整合durid 1.添加druid连接池maven依赖 <dependency> <groupId> ...
- Mybatis 搭配 阿里druid连接池 连接 oracle 或 mysql
DRUID介绍 DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针 ...
- SpringBoot2.0 基础案例(07):集成Druid连接池,配置监控界面
一.Druid连接池 1.druid简介 Druid连接池是阿里巴巴开源的数据库连接池项目.Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能.功能强大,能防SQL注入,内置Login ...
- spring 5.x 系列第6篇 —— 整合 mybatis + druid 连接池 (代码配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 项目目录结构 1.创建maven工程,除了Spring基本依赖外,还需要导 ...
随机推荐
- 求解: Windows Phone XAML Controls 为什么是disable状态?
问题 : 我在做一个windows phone 的App,显示一个web 返回来的data,现在想用控件ListView 去绑定这个Data,但是 为何我的VS2012 中的 ToolBox 的XAM ...
- 【我们一起写框架】MVVM的WPF框架(四)—DataGrid
前言 这个框架写到这里,应该有很多同学发现,框架很多地方的细节,其实是违背了MVVM的设计逻辑的. 没错,它的确是违背了. 但为什么明知道违背设计逻辑,还要这样编写框架呢? 那是因为,我们编写的是框架 ...
- JDBC事务控制
概念 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用户程序的执行所引起,并 ...
- 第十一课 CSS介绍与font字体 css学习1
一.CSS样式规则 1.基本结构 <html> <head> <style> h1{ color: orange; } </style> </he ...
- adb 查看 android手机的CPU架构
adb shell cat /proc/cpuinfo 当然要下载adb并配置好环境变量
- java:编程比赛中有用的方法整理(一)数组
我曾经参加过几次编程比赛,但是当时用的是c语言,现在学习了java,打算专攻java组,故以此整理. 数组无论在哪里都必不可少. 一.数组的拷贝: 使用Arrays类的copyOf方法: 1.将一个数 ...
- Python使用Plotly绘图工具,绘制甘特图
今天来讲一下如何使用Python 的绘图工具Plotly来绘制甘特图的方法 甘特图大家应该了解熟悉,就是通过条形来显示项目的进度.时间安排等相关情况的. 我们今天来学习一下,如何使用ployly来绘制 ...
- 安装可以查看PMM 源码的Go环境
1.基础介绍 最近在搭建PMM数据库监控系统,我们知道 Prometheus 是 PMM Server 的重要组件,*_exporter是PMM Client的主要组件. 归属组件 名称 作用 Ser ...
- (五)图数据库数neo4j据备份与恢复
1.备份方式 neo4j目前有三种备份方式: (1)java在线备份,通过java程序可在neo4j启动状态下备份数据,也可远程备份(社区版本目前不支持) (2)neo4j-admin工具,可在neo ...
- 专注于C#.Net WPF软件开发-软件反编译-软件破解-逆向-靖芯科技-包括安卓APK反编译
靖芯科技提供.Net软件开发,软件修改定制二次开发,软件破解,反编译,逆向等各项优质服务: 包括安卓APK软件反编译. 包括但不限于C#,WPF,Surface,Winform,Asp.net.JAV ...