我是靠谱客的博主 重要唇膏,最近开发中收集的这篇文章主要介绍druid连接池_springboot三步整合阿里数据库连接池druid,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

每天分享java知识干货, 欢迎关注转发评论交流,每天一点进步!

  • 第一步pom.xml 如下图:
d6258d4774ceb59e750820b1867a0be2.png
  • 第二步druid.properities 如下图:
ecbc23d0b172a6a0cf1c1ed2960ace47.png
  • 第三步:druidConfig.java
e641b93978f95d7b26ce0276777da46e.png
60fe5005c099b2fbf6e398ff216743d9.png
4f2b9f0d1442b3630024e58e16629452.png
de4d007ac1780ad3608fcf2c36509888.png
  • 最后看下效果:http://localhost:9090/demo/druid/index.html
93d1d1e590d919cf1e308b9b9e06757f.png

最后上下源代码,自己复制到本地 试试效果

  • driud.properties
#spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriverspring.datasource.driver-class-name=com.mysql.jdbc.Driver#spring.datasource.url=jdbc:oracle:thin:@localhost:1521/orclspring.datasource.url=jdbc:mysql://localhost:3306/demospring.datasource.username=rootspring.datasource.password=123456#初始化连接大小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 #申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性 spring.druid.testWhileIdle: true  #获取连接时执行检测,建议关闭,影响性能 spring.druid.testOnBorrow: false  #归还连接时执行检测,建议关闭,影响性能 spring.druid.testOnReturn: false  #是否开启PSCache,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭 spring.druid.poolPreparedStatements: false #开启poolPreparedStatements后生效 spring.druid.maxPoolPreparedStatementPerConnectionSize: 20 #配置扩展插件,常用的插件有=>stat:监控统计 log4j:日志 wall:防御sql注入 spring.druid.filters: stat,wall,log4j#通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.druid.connectionProperties: 'druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000' 
  • DruidConfig.java
package com.junsi.demo.config;import java.sql.SQLException;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Value;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 org.springframework.context.annotation.Primary;import org.springframework.context.annotation.PropertySource;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter;/**  * 连接池配置 * @author pzx * 2019年6月16日 */@Configuration@PropertySource("classpath:druid.properties")public class DruidConfig {  @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") 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 @Primary public 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; } /** * 后台管理员监控相关参数设置 * http://localhost:9090/demo/druid/login.html * @return */ @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); servletRegistrationBean.addInitParameter("allow

最后

以上就是重要唇膏为你收集整理的druid连接池_springboot三步整合阿里数据库连接池druid的全部内容,希望文章能够帮你解决druid连接池_springboot三步整合阿里数据库连接池druid所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部