springboot 使用DruidDataSource 数据源
一、添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
二、配置application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mxntest?characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 50
# 配置获取连接等待超时的时间
maxWait: 60000
三、Druid数据源配置
package com.example.demo.config;
/**
* @author 12084
* @create 2018-08-09 11:27
*/
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
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 org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* Druid数据源配置
*/
@Configuration
public class DataSourceConfig {
private static String dbUrl;
private static String username;
private static String password;
private static String driverClassName;
private static int initialSize;
private static int minIdle;
private static int maxActive;
private static int maxWait;
/**
* 注册DruidServlet
*
* @return
*/
@Bean
public ServletRegistrationBean druidServletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new StatViewServlet());
servletRegistrationBean.addUrlMappings("/druid/*");
//登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
return servletRegistrationBean;
}
/**
* 注册DruidFilter拦截
*
* @return
*/
@Bean
public FilterRegistrationBean druidFilterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
Map<String, String> initParams = new HashMap<String, String>();
//设置忽略请求
initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
filterRegistrationBean.setInitParameters(initParams);
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
/**
* 配置DataSource
* @return
* @throws SQLException
*/
@Bean(initMethod = "init",destroyMethod = "close")
@Primary
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setUrl(dbUrl);
druidDataSource.setFilters("stat,wall");
druidDataSource.setInitialSize(initialSize);
druidDataSource.setMinIdle(minIdle);
druidDataSource.setMaxActive(maxActive);
druidDataSource.setMaxWait(maxWait);
druidDataSource.setUseGlobalDataSourceStat(true);
druidDataSource.setDriverClassName(driverClassName);
return druidDataSource;
}
@Value("${spring.datasource.url}")
public void setDbUrl(String dbUrl) {
DataSourceConfig.dbUrl = dbUrl;
}
@Value("${spring.datasource.username}")
public void setUsername(String username) {
DataSourceConfig.username = username;
}
@Value("${spring.datasource.password}")
public void setPassword(String password) {
DataSourceConfig.password = password;
}
@Value("${spring.datasource.driver-class-name}")
public void setDriverClassName(String driverClassName) {
DataSourceConfig.driverClassName = driverClassName;
}
@Value(value = "${spring.datasource.initialSize}")
public void setInitialSize(int initialSize) {
DataSourceConfig.initialSize = initialSize;
}
@Value(value = "${spring.datasource.minIdle}")
public void setMinIdle(int minIdle) {
DataSourceConfig.minIdle = minIdle;
}
@Value(value = "${spring.datasource.maxActive}")
public void setMaxActive(int maxActive) {
DataSourceConfig.maxActive = maxActive;
}
@Value(value = "${spring.datasource.maxWait}")
public void setMaxWait(int maxWait) {
DataSourceConfig.maxWait = maxWait;
}
}
四、http://localhost:8080/druid/index.html 就可以查看,如果配置密码则输入密码
//登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
springboot 使用DruidDataSource 数据源的更多相关文章
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- SpringBoot整合Druid数据源
关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...
- SpringBoot 的多数据源配置
最近在项目开发中,需要为一个使用 MySQL 数据库的 SpringBoot 项目,新添加一个 PLSQL 数据库数据源,那么就需要进行 SpringBoot 的多数据源开发.代码很简单,下面是实现的 ...
- springboot添加多数据源连接池并配置Mybatis
springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018 ...
- springboot之多数据源配置JdbcTemplate
springboot多数据源配置,代码如下 DataSourceConfig package com.rookie.bigdata.config; import org.springframework ...
- springboot+ibatis 多数据源配置
这个是boot基本版本包,因为我用的打包方式是war所以去除掉了boot内置的tomcat,但是为了方便测试又引入了内置tomcat,只要添加<scope>provided</sco ...
- spring-boot (四) springboot+mybatis多数据源最简解决方案
学习文章来自:http://www.ityouknow.com/spring-boot.html 配置文件 pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置: mybatis.confi ...
- springBoot整合多数据源
springBoot整合相关 1:springBoot整合多数据源: 应用场景: 项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...
- springboot添加多数据源 以及 动态添加数据源动态切换数据源
<!-- Druid 数据连接池依赖 --> <dependency> <groupId>com.alibaba</groupId> <artif ...
随机推荐
- Linux系统安装JDK8
一.卸载现用的JDK 1.查看Linux自带的JDK是否已安装 查看是否安装openjdk,java -version (yum安装的 一般都是 OpenJDK 命令:yum install ...
- 万变不离其宗之UART要点总结
[导读] 单片机开发串口是应用最为广泛的通信接口,也是最为简单的通信接口之一,但是其中的一些要点你是否明了呢?来看看本人对串口的一些总结,当然这个总结并不能面面俱到,只是将个人认为具有共性以及相对比较 ...
- 【高并发】面试官问我如何使用Nginx实现限流,我如此回答轻松拿到了Offer!
写在前面 最近,有不少读者说看了我的文章后,学到了很多知识,其实我本人听到后是非常开心的,自己写的东西能够为大家带来帮助,确实是一件值得高兴的事情.最近,也有不少小伙伴,看了我的文章后,顺利拿到了大厂 ...
- day29 继承
目录 一.property装饰器 应用场景1 应用场景2 应用场景3(场景2优化) 二.继承介绍 1 语法 2 属性查找 3 继承的实现原理 3.1 菱形问题 3.2 继承原理 3.3 深度优先和广度 ...
- JavaScript学习 Ⅴ
十. 一些对象 Date 对象 Date对象用来表示一个时间 创建Date对象 如果直接使用构造函数创建一个Date对象,则会封装为当前代码执行的时间 var d = new Date(); 创建一个 ...
- CTF_show平台 web题解 part3
web13 题目显示文件上传,各类型上传均提示错误,在使用ctf-scan扫描的时候,发现upload.php.bak. 查看源码: <?php header("content-typ ...
- 数据可视化基础专题(九):Matplotlib 基础(一)坐标相关
1.前言 图表要素如下图所示 # sphinx_gallery_thumbnail_number = 3 import matplotlib.pyplot as plt import numpy as ...
- flask源码剖析系列(系列目录)
flask源码剖析系列(系列目录) 01 flask源码剖析之werkzurg 了解wsgi 02 flask源码剖析之flask快速使用 03 flask源码剖析之threading.local和高 ...
- Centos7之LNMP环境编译安装
Centos7之LNMP环境编译安装 一.系统环境准备 注:安装时间过长,只做参考!!!1.系统信息 [root@localhost ~]# uname -r 3.10.0-957.el7.x86_6 ...
- Git管理修改、撤销和删除文件
目录 备注: 知识点 管理修改 撤销修改 没有git add添加到暂存区时的撤销 git add添加到暂存区后的撤销 git commit提交后的撤销 删除文件 确定从版本库中删除文件 从暂存区把误删 ...