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

1.导包

<!-- druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!-- log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>

2.添加配置

修改application.yml

spring:datasource:name: druidDataSourcetype: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mango?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTCusername: rootpassword: 101800filters: stat,wall,log4j,configmax-active: 100initial-size: 1max-wait: 60000min-idle: 1time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000validation-query: select 'x'test-while-idle: truetest-on-borrow: falsetest-on-return: falsepool-prepared-statements: truemax-open-prepared-statements: 50max-pool-prepared-statement-per-connection-size: 20

添加配置类

package com.chq.mango.config;import org.springframework.boot.context.properties.ConfigurationProperties;/*** 数据源属性*/
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DruidDataSourceProperties {// jdbcprivate String driverClassName;private String url;private String username;private String password;// jdbc connection poolprivate int initialSize;private int minIdle;private int maxActive = 100;private long maxWait;private long timeBetweenEvictionRunsMillis;private long minEvictableIdleTimeMillis;private String validationQuery;private boolean testWhileIdle;private boolean testOnBorrow;private boolean testOnReturn;private boolean poolPreparedStatements;private int maxPoolPreparedStatementPerConnectionSize;// filterprivate String filters;public int getInitialSize() {return initialSize;}public void setInitialSize(int initialSize) {this.initialSize = initialSize;}public int getMinIdle() {return minIdle;}public void setMinIdle(int minIdle) {this.minIdle = minIdle;}public int getMaxActive() {return maxActive;}public void setMaxActive(int maxActive) {this.maxActive = maxActive;}public long getMaxWait() {return maxWait;}public void setMaxWait(long maxWait) {this.maxWait = maxWait;}public long getTimeBetweenEvictionRunsMillis() {return timeBetweenEvictionRunsMillis;}public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;}public long getMinEvictableIdleTimeMillis() {return minEvictableIdleTimeMillis;}public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;}public String getValidationQuery() {return validationQuery;}public void setValidationQuery(String validationQuery) {this.validationQuery = validationQuery;}public boolean isTestWhileIdle() {return testWhileIdle;}public void setTestWhileIdle(boolean testWhileIdle) {this.testWhileIdle = testWhileIdle;}public boolean isTestOnBorrow() {return testOnBorrow;}public void setTestOnBorrow(boolean testOnBorrow) {this.testOnBorrow = testOnBorrow;}public boolean isTestOnReturn() {return testOnReturn;}public void setTestOnReturn(boolean testOnReturn) {this.testOnReturn = testOnReturn;}public boolean isPoolPreparedStatements() {return poolPreparedStatements;}public void setPoolPreparedStatements(boolean poolPreparedStatements) {this.poolPreparedStatements = poolPreparedStatements;}public int getMaxPoolPreparedStatementPerConnectionSize() {return maxPoolPreparedStatementPerConnectionSize;}public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;}public String getFilters() {return filters;}public void setFilters(String filters) {this.filters = filters;}public String getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

3.配置Servlet和Filter

package com.chq.mango.config;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.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.servlet.Filter;
import javax.servlet.Servlet;
import javax.sql.DataSource;
import java.sql.SQLException;/*** Druid数据源配置*/
@Configuration
@EnableConfigurationProperties({DruidDataSourceProperties.class})
public class DruidConfig {@Autowiredprivate DruidDataSourceProperties properties;@Bean@ConditionalOnMissingBeanpublic DataSource druidDataSource() {DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setDriverClassName(properties.getDriverClassName());druidDataSource.setUrl(properties.getUrl());druidDataSource.setUsername(properties.getUsername());druidDataSource.setPassword(properties.getPassword());druidDataSource.setInitialSize(properties.getInitialSize());druidDataSource.setMinIdle(properties.getMinIdle());druidDataSource.setMaxActive(properties.getMaxActive());druidDataSource.setMaxWait(properties.getMaxWait());druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());druidDataSource.setValidationQuery(properties.getValidationQuery());druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());druidDataSource.setTestOnReturn(properties.isTestOnReturn());druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());try {druidDataSource.setFilters(properties.getFilters());druidDataSource.init();} catch (SQLException e) {e.printStackTrace();}return druidDataSource;}/*** 注册Servlet信息, 配置监控视图** @return*/@Bean@ConditionalOnMissingBeanpublic ServletRegistrationBean<Servlet> druidServlet() {ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<Servlet>(new StatViewServlet(), "/druid/*");//白名单:
//        servletRegistrationBean.addInitParameter("allow","127.0.0.1,139.196.87.48");//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.servletRegistrationBean.addInitParameter("deny","192.168.1.119");//登录查看信息的账号密码, 用于登录Druid监控后台servletRegistrationBean.addInitParameter("loginUsername", "admin");servletRegistrationBean.addInitParameter("loginPassword", "admin");//是否能够重置数据.servletRegistrationBean.addInitParameter("resetEnable", "true");return servletRegistrationBean;}/*** 注册Filter信息, 监控拦截器** @return*/@Bean@ConditionalOnMissingBeanpublic FilterRegistrationBean<Filter> filterRegistrationBean() {FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<Filter>();filterRegistrationBean.setFilter(new WebStatFilter());filterRegistrationBean.addUrlPatterns("/*");filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}
}

4.编译运行

登陆+密码都admin,访问:http://localhost:8001/druid/login.html

 

权限管理系统4—集成Druid数据源相关推荐

  1. 权限管理系统2—集成Swagger文档

    (Swagger介绍及使用) Swagger用于多人协作中共享和及时更新API开发接口文档的问题 1.  引入包 <!-- swagger --><dependency>< ...

  2. 权限管理系统3—集成MyBatis框架

    1. 导包 <!-- mybatis --><dependency><groupId>org.mybatis.spring.boot</groupId> ...

  3. 从零开始打造一款基于SpringBoot+SpringCloud的后台权限管理系统

    随着 Spring Boot 和 Spring Cloud 的诞生和流行,集智慧于大成的 Spring 技术体系成为行业开发的首选之一.市场代表需求,技术代表能力.显而易见,在当今开发领域中,谁能更好 ...

  4. 权限管理系统项目文档——SpringBoot后端

    文章目录 关键技术 第一篇 后端实现篇 1. 搭建开发环境 2. 集成Swagger文档 3. 集成MyBatis框架 4. 集成Druid数据源 5. 跨域解决方案 6. 业务功能实现 6.1 工程 ...

  5. SpringBoot整合JDBC、整合Druid数据源详解教程

    目录 一.整合JDBC 1. 环境准备 1. 创建数据库 2. 创建SpringBoot项目 3. IDEA连接数据库 2. 编写数据库配置信息 3. 编写测试类测试 4. CRUD操作数据库 1. ...

  6. boot druid 长时间不连接 异常_Spring Boot学习:如何使用Druid数据源

    Druid概述 Druid是阿里巴巴开源的一款非常优秀的数据库连接池.在Java应用程序开发中,常用的连接池还有DBCP.C3P0.Proxool等. SpringBoot2.X 版本开始默认的是Hi ...

  7. springboot15 集成Druid(德鲁伊)

    15.整合Druid 15.1.Druid简介 Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池. Druid 是阿里巴巴开源平台上一个数据库连接池实现,结合 ...

  8. 通用权限管理系统组件 中集成多个子系统的单点登录(网站入口方式)附源码

    通用权限管理系统组件 (GPM - General Permissions Manager) 中集成多个子系统的单点登录(网站入口方式)附源码 上文中实现了直接连接数据库的方式,通过配置文件,自定义的 ...

  9. 如何在自己的信息管理系统里集成第三方权限控制组件 - 设计一个漂亮的WEB界面...

    我们大家都梦想有个完美的各种信息管理系统,其实一个人又会数据库,又会C#.NET程序,还要精通HTML,还要精通CSS,更要精通JS,还有精力去写很多东西,又要调试前台又要调试后台,而且每开发一个系统 ...

最新文章

  1. Web性能优化之雅虎军规
  2. asp.net中读取带有加号(+)的Cookie,会自动把加号替换为空格
  3. python列表list的基本性质
  4. asp.net Forums 之HttpHandler和HttpModule
  5. 推荐:周志华《机器学习》西瓜书精炼版笔记来了!
  6. 【RAC】 RAC For W2K8R2 安装--总体规划 (一)
  7. 【设计模式】装饰器模式的使用
  8. HTML5应用 + Cordova = 平台相关的混合应用
  9. Eclipse设置控制台日志输出位置
  10. 【APICloud系列|12】ios真机调试时如何添加新设备的udid?
  11. 阿里开发者们的第15个感悟:做一款优秀大数据引擎,要找准重点解决的业务场景
  12. win7系统定时删除数据的批处理命令_使用bat批处理命令清理windows7系统垃圾文件...
  13. mysql配置所有ip连接_Mysql查看用户连接数配置及每个IP的请求情况
  14. 百度正用谷歌AlphaGo,解决一个比围棋更难的问题 | 300块GPU在燃烧
  15. [android]netd与NetworkManagementService初印象
  16. Python游戏编程入门(一)——初识Pygame
  17. 大数据算法 十大经典算法
  18. 如何锁定计算机桌面图标,解决win7、win10系统怎么锁定电脑桌面图标
  19. 移动前端开发和 Web 前端开发的区别
  20. 世界传说 换装迷宫2 所有人物及所有技能及奖励技能 传说系列各秘奥技和台词

热门文章

  1. 费斯托FB33阀岛简介
  2. IHS安装ssl证书
  3. 软通动力子公司携“SwanLinkOS商业PC发行版”亮相OpenHarmony开发者大会
  4. Linux安装后的任务
  5. pdf转ppt简单方法
  6. TC (Teamcenter) 许可证解决方案
  7. 如何解决过拟合问题?
  8. 实现app短信验证码功能这样做就很简单!
  9. 关于磷酸铁锂电池充满电后电压回落的问题
  10. LeetCode 682 棒球比赛