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

废话少说,按SpringBoot的老套路来。

【step1】:添加依赖

<!-- 数据库连接池  -->
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.25</version>
</dependency>

 

【step2】:application.yml中添加对应配置

spring:datasource:driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://对应自己的数据库连接
    username: xxxpassword: xxx
######################### Druid连接池的配置信息  #################
spring.druid.initialSize: 5                                 #初始化连接大小
spring.druid.minIdle: 5                                     #最小连接池数量
spring.druid.maxActive: 20                                  #最大连接池数量
spring.druid.maxWait: 60000                                 #获取连接时最大等待时间,单位毫秒
spring.druid.timeBetweenEvictionRunsMillis: 60000           #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.druid.minEvictableIdleTimeMillis: 300000             #配置一个连接在池中最小生存的时间,单位是毫秒
spring.druid.validationQuery: SELECT 1 FROM DUAL            #测试连接
spring.druid.testWhileIdle: true                            #申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性
spring.druid.testOnBorrow: false                            #获取连接时执行检测,建议关闭,影响性能
spring.druid.testOnReturn: false                            #归还连接时执行检测,建议关闭,影响性能
spring.druid.poolPreparedStatements: false                  #是否开启PSCache,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭
spring.druid.maxPoolPreparedStatementPerConnectionSize: 20  #开启poolPreparedStatements后生效
spring.druid.filters: stat,wall,log4j                       #配置扩展插件,常用的插件有=>stat:监控统计  log4j:日志  wall:防御sql注入
spring.druid.connectionProperties: 'druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000'  #通过connectProperties属性来打开mergeSql功能;慢SQL记录

 

【step3】:java代码中读取配置,并做相关转换(比如数据库密码加解密等,这里省略)

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;@Configuration
public class DruidConfiguration {@Value("${spring.datasource.url}")private String url;@Value("${spring.datasource.username}")private String username;@Value("${spring.datasource.password}")private String password;@Value("${spring.datasource.driverClassName}")private String driverClassName;@Value("${spring.druid.initialSize}")private int initialSize;@Value("${spring.druid.minIdle}")private int minIdle;@Value("${spring.druid.maxActive}")private int maxActive;@Value("${spring.druid.maxWait}")private int maxWait;@Value("${spring.druid.timeBetweenEvictionRunsMillis}")private int timeBetweenEvictionRunsMillis;@Value("${spring.druid.minEvictableIdleTimeMillis}")private int minEvictableIdleTimeMillis;@Value("${spring.druid.validationQuery}")private String validationQuery;@Value("${spring.druid.testWhileIdle}")private boolean testWhileIdle;@Value("${spring.druid.testOnBorrow}")private boolean testOnBorrow;@Value("${spring.druid.testOnReturn}")private boolean testOnReturn;@Value("${spring.druid.poolPreparedStatements}")private boolean poolPreparedStatements;@Value("${spring.druid.maxPoolPreparedStatementPerConnectionSize}")private int maxPoolPreparedStatementPerConnectionSize;@Value("${spring.druid.filters}")private String filters;@Value("{spring.druid.connectionProperties}")private String connectionProperties;@Bean@Primarypublic DataSource dataSource() {DruidDataSource datasource = new DruidDataSource();datasource.setUrl(url);datasource.setUsername(username);datasource.setPassword(password);   //这里可以做加密处理
        datasource.setDriverClassName(driverClassName);//configuration
        datasource.setInitialSize(initialSize);datasource.setMinIdle(minIdle);datasource.setMaxActive(maxActive);datasource.setMaxWait(maxWait);datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);datasource.setValidationQuery(validationQuery);datasource.setTestWhileIdle(testWhileIdle);datasource.setTestOnBorrow(testOnBorrow);datasource.setTestOnReturn(testOnReturn);datasource.setPoolPreparedStatements(poolPreparedStatements);datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);try {datasource.setFilters(filters);} catch (SQLException e) {}datasource.setConnectionProperties(connectionProperties);return datasource;}@Beanpublic ServletRegistrationBean statViewServlet(){ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");servletRegistrationBean.addInitParameter("allow","127.0.0.1");  //设置ip白名单servletRegistrationBean.addInitParameter("deny","192.168.0.19");//设置ip黑名单,优先级高于白名单//设置控制台管理用户servletRegistrationBean.addInitParameter("loginUsername","root");servletRegistrationBean.addInitParameter("loginPassword","root");//是否可以重置数据servletRegistrationBean.addInitParameter("resetEnable","false");return servletRegistrationBean;}@Beanpublic FilterRegistrationBean statFilter(){//创建过滤器FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());//设置过滤器过滤路径filterRegistrationBean.addUrlPatterns("/*");//忽略过滤的形式filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}
}

 

【step4】:测试

登录成功后如下:

 

转载于:https://www.cnblogs.com/funnyboy0128/p/9052447.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/397935.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python名字排序_python 中文排序(按拼音)

pypinyin1 排序 仅按拼音首字母 排序安装pypinyinpip3 install pypinyin代码from pypinyin import lazy_pinyinchars [鑫,鹭,榕,柘,珈,骅,孚,迦,瀚,濮,浔,沱,泸,恺,怡,岷,萃,兖]chars.sort(keylambda char: lazy_pinyin(char)[0][0])print([lazy_pinyin(char) for char in cha…

struts2中非表单标签的使用 componen

2. component标签 component标签用于使用自己的自定义组件&#xff0c;这是一个非常灵活的用法&#xff0c;如果经常需要使用某个效果片段&#xff0c;就可以考虑将这个效果片段定义成一个自定义组件&#xff0c;然后在页面中使用component标签来使用该自定义组件。因为使…

oracle 11g r2 rac中节点时间不同步,Oracle11gR2安装RAC错误之--时钟不同步

系统环境&#xff1a; 操作系统&#xff1a;RedHat EL5 Cluster&#xff1a; Oracle GI(Grid Infrastructure) Oracle&#xff1a; Oracle 11.2.0.1.0 如图所示&#xff1a;RAC 系统架系统环境&#xff1a;操作系统&#xff1a;RedHat EL5Cluster&#xff1a; Oracle GI(Grid I…

vue中pdf预览组件_Vue+ElementUI使用vue-pdf实现预览功能

Vue ElementUI项目中使用vue-pdf实现简单预览&#xff0c;供大家参考&#xff0c;具体内容如下1、安装 vue-pdfnpm install --save vue-pdf2、在vue页面中导入对应的组件我这是通过点击 预览 按钮 获取id打开一个dialog来实现:visible.sync"viewVisible" width"…

酒店预定系统

系统功能模块功能图 用例图 E-R图 转载于:https://www.cnblogs.com/qlly-20/p/9052461.html

leetcode 145. Binary Tree Postorder Traversal

传送门 145. Binary Tree Postorder Traversal QuestionEditorial SolutionMy SubmissionsTotal Accepted: 106482Total Submissions: 291244Difficulty: HardGiven a binary tree, return the postorder traversal of its nodes values. For example:Given binary tree {1,#…

74-A/D指标,Accumulation/Distribution,积累/派发线,离散指标.(2015.7.1)

A/D指标&#xff0c;Accumulation/Distribution积累/派发线&#xff0c;离散指标观井映天2015.7.1转载于:https://www.cnblogs.com/i201102053/p/10626638.html

linux 密码修改下次,问题:如何强制用户在下次登录Linux时更改密码

当你使用默认密码创建用户时&#xff0c;你必须强制用户在下一次登录时更改密码。当你在一个组织中工作时&#xff0c;此选项是强制性的。因为老员工可能知道默认密码&#xff0c;他们可能会也可能不会尝试不当行为,看到下图会不会有为用户担心的感觉&#xff1a;使用 passwd 命…

tableau获取筛选器值_认识Tableau中的筛选器

Tableau中的筛选器&#xff1a;(1)提取筛选器(2)数据源筛选器(3)上下文筛选器(4)维度筛选器(5)度量筛选器(6)参数筛选器(7)表计算筛选器(8)页面筛选器对筛选器进行简单的分类&#xff1a;数据层(提取筛选器、数据源筛选器、上下文筛选器、参数筛选器)视图层(维度筛选器、度量筛…

类与对象的实例属性

class Chinese: countrychina def __init__(self,name): self.namename def playball(self,ball): print(%s正在打%s%(self.name,ball))name1input(>>>)#输入输出尽量别放函数里p1Chinese(name1)p1.playball(足球)print(p1.country)#china,类的…

svn的merge使用例子

先说说什么是branch。按照Subversion的说法&#xff0c;一个branch是某个development line&#xff08;通常是主线也即trunk&#xff09;的一个拷贝&#xff0c;见下图&#xff1a; branch存在的意义在于&#xff0c;在不干扰trunk的情况下&#xff0c;和trunk并行开发&#xf…

C# 课堂总结2-数据类型及转换方式

一、输入输出语句 Console.ReadLine(); 会等待直到用户按下回车&#xff0c;一次读入一行Console.ReadKey(); 则是等待用户按下任意键&#xff0c;一次读入一个字符。 二、数据类型 主要掌握&#xff1a; 1.值类型&#xff1a;int 整型&#xff0c;float 浮点型&#xff08;单精…

linux编译c gedit,[2018年最新整理]LINUX-Gedit文本编辑器.ppt

[2018年最新整理]LINUX-Gedit文本编辑器Gedit文本编辑器 系别&#xff1a;电信系 班级&#xff1a;08 自动化 姓名&#xff1a;张小亚 学号&#xff1a; 30 号 辅导老师&#xff1a;兰建平 Gedit的启动与打开文本 Gedit可以用命令或主菜单的方式两种方式启动。打开文件可以在终…

jsx怎么往js里传参数_在vue中使用jsx语法的使用方法

什么是JSX?JSX就是Javascript和XML结合的一种格式。React发明了JSX&#xff0c;利用HTML语法来创建虚拟DOM。当遇到我为什么要在vue中用JSX?想折腾一下呗,开玩笑.最开始是因为近期在学习react,在里面体验了一把jsx语法,发现也并没有别人说的很难受的感觉啊,于是就想尝试在vue…

如何对数据库中的表以及表中的字段进行重命名

貌似没有直接的SQL语句可以做到这一点&#xff0c;除非删除再加。。。 SQL SERVER 中提供了存储过程&#xff1a;SP_RENAME 对表进行重命名&#xff1a; 更多详见&#xff1a;https://msdn.microsoft.com/zh-cn/library/ms188351.aspx转载于:https://www.cnblogs.com/xwgli/p/4…

系统优化-----sysctl.conf文件内核设置参数详解

摘自https://blog.csdn.net/tallercc/article/details/52823075 sysctl.conf工作原理 sysctl命令被用于在内核运行时动态地修改内核的运行参数&#xff0c;可用的内核参数在目录/proc/sys中。它包含一些TCP/IP堆栈和虚拟内存系统的高级选项&#xff0c; 这可以让有经验的管理员…

How Tomcat works — 一、怎样阅读源码

在编程的道路上&#xff0c;通过阅读优秀的代码来提升自己是很好的办法。一直想阅读一些开源项目&#xff0c;可是没有合适的机会开始。最近做项目的时候用到了shiro&#xff0c;需要做集群的session共享&#xff0c;经过查找发现tomcat的session本身就支持&#xff0c;发现自己…

linux c字符连接,C 语言实例

指针方法&#xff1a;#include int main(){char str1[100], str2[100], *p str1,n 0;printf("请输入第一个字符串:");scanf("%s", str1);printf("请输入第二个字符串:");scanf("%s", str2);while (*p ! \0);/*移动指针到str1尾*/--p…

plot画分段函数_python画图函数大全

很多时候&#xff0c;我们需要用python画图&#xff0c;这样就可以更加直观的看到数据的走势&#xff0c;而不是干巴巴的数字。今天&#xff0c;我们就给大家整理了一下python画图的常用函数&#xff0c;由于篇幅限制。无法将这些函数的使用方法全部表现出来。所以&#xff0c;…