关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html

一:Druid介绍

1. Druid是什么?

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

2. 在哪里下载druid

3. 怎么获取Druid的源码

Druid是一个开源项目,源码托管在github上,源代码仓库地址是 https://github.com/alibaba/druid。同时每次Druid发布正式版本和快照的时候,都会把源码打包,你可以从上面的下载地址中找到相关版本的源码

二:SpringBoot集成

首先在你的SpringBoot项目导入依赖

1.导入依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

2.配置数据源与连接池

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
#   数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

3.注入DataSource

@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

    //配置Druid的监控
    // 1、配置一个管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet() {
        //配置参数参考ResourceServlet类
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String, String> initParams = new HashMap<>();
        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "123456");
        initParams.put("allow", "");//默认就是允许所有访问
        initParams.put("deny", "192.168.15.21");//拒绝谁
        bean.setInitParameters(initParams);
        return bean;
    }

    //2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<>();
        initParams.put("exclusions", "*.js,*.css,/druid/*");
        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}

4.启动应用

访问http://localhost:8080/druid体验强大的Druid!

官方Spring boot集成文档:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

SpringBoot整合Druid数据源的更多相关文章

  1. SpringBoot整合jdbc及整合Druid数据源

    一.整合jdbc 1.创建一个springInitializr项目 勾选 web----springweb.SQL----JDBC API,MYSQL Diver 2.连接数据库 3.创建yml 4. ...

  2. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  3. Springboot整合druid

    目录 Springboot整合druid application.yml DruidConfig 数据监控地址:http://localhost:8080/druid Springboot整合drui ...

  4. SpringBoot整合Druid数据连接池

    SpringBoot整合Druid数据连接池 Druid是什么? Druid是Alibaba开源的的数据库连接池.Druid能够提供强大的监控和扩展功能. 在哪里下载druid maven中央仓库: ...

  5. 【SpringBoot | Druid】SpringBoot整合Druid

    SpringBoot整合Druid Druid是个十分强大的后端管理工具,具体的功能和用途请问阿里爸爸 1. 在pom.xml中导入包 <!-- alibaba 的druid数据库连接池 --& ...

  6. springBoot整合多数据源

    springBoot整合相关 1:springBoot整合多数据源: 应用场景:     项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...

  7. springboot 配置DRUID数据源

    druid 是阿里开源的数据库连接池. 开发时整合   druid 数据源过程. 1.修改pom.xml <dependency> <groupId>mysql</gro ...

  8. springboot整合druid、mybatis

    目的: 1.springboot配置数据库连接池druid 测试druid中url监控 2.springboot整合mybatis 测试查删案例 3.springboot整合pagehelper sp ...

  9. springboot整合druid和配置资源监控

    1.添加依赖,在maven repository中搜索 <dependency> <groupId>com.alibaba</groupId> <artifa ...

随机推荐

  1. Spring整合ActiveMq消息队列

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久 ...

  2. day11(python)装饰器

    def wrapper(f):#1 def inner(*args,**kwargs):#3 ret = f(*args,**kwargs)#5 return ret#8 return inner#4 ...

  3. DAY4(python)打印字符串以及增删改查

    用while循环打印字符串 #if i in s: # print ( i ) s='nanfjkhndaol' index = 0 while 1 : print (s[index]) index+ ...

  4. 关于C#List中FindAll用法的一些简单示例

    using System; using System.Collections.Generic; public partial class List : System.Web.UI.Page { pro ...

  5. jenkins 备份配置信息

    本文介绍几种备份jenkin配置信息的方法,大家可根据实际情况做出选择. 我的测试环境如下: windows 7 jenkins 2.32.3 ____升级到___2.46.3 (长期支持版本) 多种 ...

  6. 特别篇:Hyper-v群集模拟实战演示

    介绍 由于前面几张的都是直接整理了下 九叔的hyper-v电子书发上来的,个人觉得他写的不是最详细,因此今天我按照自己的实际情况来写个模拟的实战演示.所有的东西都通过VMware WorkStatio ...

  7. .net的mvc的fw版本为4.5发布到阿里云【云虚拟主机】上.

    注意:云虚拟主机和云服务器(ECS)不是同一个产品,请注意分别. 云服务器ECS: 云虚拟主机: 我用的是云虚拟主机也是第二个,版本是window server  声明:默认,已经把域名[已备案]绑定 ...

  8. 【转】cookie如何共享到各个浏览器

    可以考虑HTML5 localstorage, 点击查看原始尺寸 http://www.cnblogs.com/xiaowei0705/archive/2011/04/19/2021372.html也 ...

  9. mumu安卓模拟器使用教程

    安装教程: 1http://mumu.163.com/  在这网址里面下载 2.安装 我的是mac 安装时需要输入你的你电脑密码  但是也报错,你点击旁边的提示就会告诉去安全与隐私里点击允许就好了 很 ...

  10. JavaScript模块载入框架sea.js 学习一

    简单总结sea.js 学习 文件文件夹结构   /sea/sea.js      下载地址  http://seajs.org/docs/#downloads   /sea/jquery-sea.js ...