1)、引入外部的数据源(Druid)

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.</version>
</dependency>

2)、配置文件中切换默认的数据源

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/users?serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

3)、配置其他属性

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/users?serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#其他配置
# 下面为连接池的补充设置,应用到上面所有数据源中
spring.datasource.initialSize=
spring.datasource.minIdle=
spring.datasource.maxActive=
# 配置获取连接等待超时的时间
spring.datasource.maxWait=
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=
spring.datasource.validationQuery=SELECT FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall
spring.datasource.logSlowSql=true

4)、此时的配置并不能使用,需要将其加入容器
@Configuration
public class MyDruid {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new
DruidDataSource();
}

}

Debug查看:

5)、配置监听

public class StatViewServlet extends ResourceServlet {
private static final Log LOG = LogFactory.getLog(StatViewServlet.class);
private static final long serialVersionUID = 1L;
public static final String PARAM_NAME_RESET_ENABLE = "resetEnable";
public static final String PARAM_NAME_JMX_URL = "jmxUrl";
public static final String PARAM_NAME_JMX_USERNAME = "jmxUsername";
public static final String PARAM_NAME_JMX_PASSWORD = "jmxPassword";
public abstract class ResourceServlet extends HttpServlet {
private static final Log LOG = LogFactory.getLog(ResourceServlet.class);
public static final String SESSION_USER_KEY = "druid-user";
public static final String PARAM_NAME_USERNAME = "loginUsername";
public static final String PARAM_NAME_PASSWORD = "loginPassword";
public static final String PARAM_NAME_ALLOW = "allow";
public static final String PARAM_NAME_DENY = "deny";
public static final String PARAM_REMOTE_ADDR = "remoteAddress";

上面作为参考!!!!

@Configuration
public class MyDruid { @ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//配置Druid的监控
//1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>(); initParams.put("loginUsername","admin");
initParams.put("loginPassword","");
//允许访问,默认所有都可访问
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;
}

使用上述设置的账号密码进行登录:

登录成功之后:

执行一个查询:

查看监控:

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

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

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

  2. 整合Druid数据源

    pom依赖: <!--引入druid数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> & ...

  3. SpringBoot整合Druid数据源

    关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...

  4. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  5. springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置

    一.整合Druid数据源 Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库 ...

  6. springboot 配置DRUID数据源

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

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

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

  8. Druid数据源的使用

    1 Druid数据源简介 Druid是Java语言中最好的数据库连接池.Druid能够提供强大的监控和扩展功能.通过访问http://localhost:8080(自己的端口)/druid/ 可以查看 ...

  9. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

随机推荐

  1. springmvc4集成swagger2

    首先在原有的springmvc工程的pom文件中增加swagger <dependency> <groupId>io.springfox</groupId> < ...

  2. linux下查看内存的使用情况

    windows上有各种软件可以进行“一键加速”之类的操作,释放掉一些内存(虽然我暂时不知道是怎么办到的,有待后续学习).而任务管理器也可以很方便地查看各进程使用的内存情况,如下图: 同样地,linux ...

  3. 关于移动web开发过程中的”点透“问题

    先说说故事发生的场景,举个栗子如下图: A是遮罩层,B是正常的DOM,C是B上的某个元素,这里是链接.场景是点击A的时候A消失,结果点到了C,页面发生了跳转,这显然不是咱想要的~ 下面我们来监测点击事 ...

  4. Strapi 安装易错位置

    Strapi官网(https://strapi.io)介绍:最先进的开源内容管理框架,可以毫不费力地构建功能强大的API,建立在Node.js平台之上,为您的API提供高速惊人的表现. 简单点说,(对 ...

  5. Python-模块3-re

    今天我们就说一个模块,那就是re,不过想要了解re模块,我们得先了解一下什么是正则表达式,有助于我们更好的学习re模块 一.正则表达式 首先, 我们在网页上进行注册或者登陆的时候经常能看到一些格式上的 ...

  6. angular ng指令

    1.指令 ng-app,ng- 都是angular的指令系统ng-app: ng-app是angular的初始化,一个页面只能有一个ng-app,位置不限制.在页面上加入了这个执行,那么从当前的元素以 ...

  7. openlayers模仿google地图--地图版权随鹰眼关闭打开而改变位置

    额..题目有点长......今天有个群友问我.想实现google地图地图版权随鹰眼关闭状态改变位置的功能.就是这种<ignore_js_op> 打开鹰眼时  地图版权也随着鹰眼位置改变而改 ...

  8. springmvc封装list个数限制问题

    提交一颗树,三级区域个数大于1000个导致提交失败!!! org.springframework.beans.InvalidPropertyException: Invalid property 'd ...

  9. SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...

  10. 116.001 - 爱折腾之用 Kindle 读学术论文是什么体验?

    @(116 - Kindle 使用指南) 结论先行 - 强烈安利k2pdfopt,把双栏论文转成kindle友好的pdf 整理转载自知乎@ wei huang 双栏学术论文在6寸屏上看就是个坑 新买的 ...