SpringBoot整合Druid 原来这么容易


idea创建springboot项目

选择相关依赖

导入相关依赖

<!--日志文件--><dependencies><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.21</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

在application.yaml(由application.properties修改而成)中添加如下配置文件

spring:datasource:username: rootpassword: passwordurl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource# 初始化大小,最小,最大initialSize: 1minIdle: 3maxActive: 20# 配置获取连接等待超时的时间maxWait: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒minEvictableIdleTimeMillis: 30000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: false# 打开PSCache,并且指定每个连接上PSCache的大小poolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filters: stat,wall,log4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500# 合并多个DruidDataSource的监控数据useGlobalDataSourceStat: true

添加日志文件配置

  • 创建log4j.properties
  • 添加如下配置
log4j.rootLogger=debug, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.loglog4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%nlog4j.logger.com.codefutures=DEBUG

创建配置类


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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 javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;@Configuration
public class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource druidDataSource(){return new DruidDataSource();}@Beanpublic ServletRegistrationBean statViewServlet(){ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");HashMap<String, String> initParameters = new HashMap<>();initParameters.put("loginUsername","admin");initParameters.put("loginPassword","123456");initParameters.put("allow","");
//       禁止访问对象 initParameters.put("deny","127.0.0.1");bean.setInitParameters(initParameters);return bean;}@Beanpublic FilterRegistrationBean webStatFilter(){FilterRegistrationBean bean = new FilterRegistrationBean();bean.setFilter(new WebStatFilter());Map<String, String> initParameters = new HashMap<>();initParameters.put("exclusions","*.js,*.css,/druid/*");bean.setInitParameters(initParameters);return bean;}
}

启动服务器,访问druid后台系统

  • 网页中输入 localhost:8080/druid

  • 如果有啥疑问,可以在评论区留言

SpringBoot整合Druid ---- 猿来如此容易相关推荐

  1. SpringBoot整合Druid连接池

    SpringBoot整合Druid连接池 前言 第一种整合方式 自定义的方式 引入Druid依赖 配置文件 配置类 实现监控功能 开启SQL防火墙 实现web应用 访问Druid页面需登录 第二种整合 ...

  2. SpringBoot 整合Druid数据源SQL监控不显示问题

    SpringBoot 整合Druid数据源SQL监控不显示问题 项目场景: 问题描述: 解决方案: 项目场景: SpringBoot 整合Druid数据源SQL监控不显示 问题描述: 明明使用Drui ...

  3. SpringBoot整合Druid,开启druid监控平台

    SpringBoot整合Druid 1.导入依赖pom.xml <!-- https://mvnrepository.com/artifact/log4j/log4j --><dep ...

  4. SpringBoot整合Druid数据源页面访问报该页面无法正常运作

    SpringBoot整合Druid数据源页面访问报该页面无法正常运作 异常页面:请求访问页面成功但是该网页无法正常运行 正常的页面: 输入:账号admin 密码:123456 进入 我的代码 pom. ...

  5. spring-boot整合druid配置

    准备:druid是阿里巴巴开源的高性能,多功能的连接池,其主要配置参数如下: 配置 缺省值 说明 name   配置这个属性的意义在于,如果存在多个数据源,监控的时候可以通过名字来区分开来.如果没有配 ...

  6. SpringBoot整合Druid数据源

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

  7. springboot整合Druid使用

    1.导入依赖 <dependency><groupId>com.alibaba</groupId><artifactId>druid</artif ...

  8. 七十、SpringBoot整合 Druid数据源

    @Author:Runsen 来源:尚硅谷 下面建议读者学习尚硅谷的B站的SpringBoot视频,我是学雷丰阳视频入门的. 具体链接如下:B站尚硅谷SpringBoot教程 文章目录 Druid连接 ...

  9. 【SpringBoot笔记】SpringBoot整合Druid数据连接池

    废话少说,按SpringBoot的老套路来. [step1]:添加依赖 <!-- 数据库连接池 --> <dependency><groupId>com.aliba ...

最新文章

  1. CS5中动作和批处理
  2. 亿级数据湖统一存储技术实践
  3. 利用现有资源快速实现汉语专用分词系统
  4. mysql left join 空值_MYSQL Left Join如何选择NULL值?
  5. iOS中常见的内存问题
  6. linux跑循环脚本占内存,Linux下实现脚本监测特定进程占用内存情况
  7. java json注解_返回json用什么注解
  8. HTML5新布局元素布局,HTML5新的布局元素
  9. mcq 队列_人工智能能力问答中的人工智能概率推理(MCQ)
  10. MDB图表统计bootstrap后台模板
  11. 路飞学城Python-Day75
  12. 2014.4.21 福州 晴 离京第一次面试(某天) 失败啊
  13. 商城项目(一) -- 项目简介、基础搭建及前端部分
  14. linux模拟发包工具,发包开源工具TRex在IPS测试中的应用
  15. ZooKeeper 客户端: GUI+命令行两大类(史上最全,值得收藏)
  16. 线性代数 第二章 矩阵 知识点总结(Jeff自我感悟)
  17. C语言入门练习— —累乘
  18. PKI密码学学习笔记
  19. linux 挂载nas网络存储_Linux挂载NAS
  20. The color “baseBlac in values has no declaration in the base values folder this can lead to crash

热门文章

  1. 解决 HDFS副本数不足问题
  2. 书籍 -- 《高性能MySQL》持续更新中(四)
  3. Python读文件与写文件
  4. 离散数学-集合论-函数(10)
  5. Shell/Linux使用Jq操作Json
  6. LC振荡电路以及考虑寄生参数时MOS管开通关断分析
  7. 04-项目立项:项目方案、可行性分析、产品规划、立项评审
  8. 千万年斗转星移,小屏幕见大宇宙 - “钦天明时” 天文时钟万年历应用程序(iOS App)说明
  9. dubbo/dubbox(一)源码编译
  10. LINUX内核编译(ZT)