Springboot整合Druid

前言

在实际的项目开发当中,为了减少多次的数据库连接给程序和数据库带来的负担,一般都会使用数据库连接池,我们今天就来介绍一下目前比较流行的数据库连接池--Druid。

Druid简介

Druid是阿里巴巴的一个开源项目,号称为监控而生的数据库连接池,功能、性能、扩展性方面都比其它例如DBCP、C3P0等连接池优秀。

Spring boot整合Druid步骤

1.添加druid依赖

java 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.20</version>
</dependency>

2.添加配置

xml 复制代码
# 开发环境下配置
spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/test?allowMultiQueries:true&characterEncoding:UTF-8&zeroDateTimeBehavior:convertToNull
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
      # 连接池配置
      initial-size: 1  # 连接池初始化连接数量
      max-active: 20  # 连接池最大活跃连接数
      min-idle: 1  # 连接池最小空闲数
      max-wait: 10000  # 配置获取连接等待超时的时间
      pool-prepared-statements: true
      max-open-prepared-statements: 20
      validation-query: SELECT 1 FROM DUAL
      validation-query-timeout: 5000
      test-on-borrow: false
      test-on-return: false
      test-while-idle: true
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 30000
      max-evictable-idle-time-millis: 60000
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,【 'stat':监控统计  'wall':用于防火墙,防御sql注入   'slf4j':日志 】
      filters: stat,wall #filters: #配置多个英文逗号分隔(统计,sql注入,log4j过滤)
    type: com.alibaba.druid.pool.DruidDataSource

接下来是@Configuration注解的配置类:

java 复制代码
@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    /**
     * 后台监控:web.xml ServletRegistrationBean
     * springBoot内置了servlet容器 所以没有web.xml 替代方法:ServletRegistrationBean
     * @return
     */
    @Bean
    public ServletRegistrationBean a(){
        ServletRegistrationBean<com.alibaba.druid.support.jakarta.StatViewServlet> bean = new ServletRegistrationBean<>(new com.alibaba.druid.support.jakarta.StatViewServlet(), "/druid/*");

        // 后台需要有人登录 账号密码配置
        HashMap<String,String> initParameters = new HashMap<>();
        // 添加配置 登录key是固定的loginUsername&loginPassword
        //账号
        initParameters.put("loginUsername","admin");
        //密码
        initParameters.put("loginPassword","admin");
        // 允许谁可以访问 ""所有人可以访问   禁止谁访问initParameters.put("deny","192.168.1.1");
        initParameters.put("allow","");
        // 设置初始化参数
        bean.setInitParameters(initParameters);
        return bean;
    }
}

到这里就已经配置完成了,在地址栏输入localhost:8080/druid就能访问druid的登录页面了。

相关推荐
踏浪无痕几秒前
JobFlow 负载感知调度:把任务分给最闲的机器
后端·架构·开源
UrbanJazzerati2 分钟前
Python自动化统计工具实战:Python批量分析Salesforce DML操作与错误处理
后端·面试
我爱娃哈哈12 分钟前
SpringBoot + Seata + Nacos:分布式事务落地实战,订单-库存一致性全解析
spring boot·分布式·后端
nil22 分钟前
记录protoc生成代码将optional改成omitepty问题
后端·go·protobuf
superman超哥1 小时前
Rust 范围模式(Range Patterns):边界检查的优雅表达
开发语言·后端·rust·编程语言·rust范围模式·range patterns·边界检查
云上凯歌1 小时前
02 Spring Boot企业级配置详解
android·spring boot·后端
秋饼2 小时前
【手撕 @EnableAsync:揭秘 SpringBoot @Enable 注解的魔法开关】
java·spring boot·后端
IT_陈寒2 小时前
Python 3.12 新特性实战:这5个改进让我的开发效率提升40%
前端·人工智能·后端
利兄的视界2 小时前
一步到位:M4 芯片 Mac 安装 PostgreSQL 16 并适配 pgvector 教程
后端·postgresql
GZKING2 小时前
ThinkPHP 8 报错"think\model\pivot" not found
后端