如何在Springboot中配置Druid连接池连接MySQL?

目录

1. 添加依赖

2. 配置文件

3. 配置类

4. 启动Druid监控

5. 访问Druid监控页面


在Springboot 中配置连接池来连接数据库是非常普遍的应用。本文主要介绍最常用的MySQL的连接用法,其中主要是使用产品开发中使用的Druid(阿里巴巴开源的)来进行示例演示如何从0到1完成数据库连接池的配置和使用。

在Spring Boot中配置Druid连接池,可以通过以下步骤实现。下面是一个完整的示例,包括pom.xml依赖、application.yml配置和Java代码。

1. 添加依赖

首先,在你的pom.xml文件中添加Druid的依赖:

<dependencies>
    <!-- Spring Boot Starter Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- Druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.8</version>
    </dependency>
    <!-- MySQL Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

2. 配置文件

application.ymlapplication.properties文件中添加Druid相关的配置。

application.yml

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/yourdatabase
      username: yourusername
      password: yourpassword
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall,log4j
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

# Druid监控页面配置
druid:
  stat-view-servlet:
    enabled: true
    url-pattern: /druid/*
    login-username: admin
    login-password: admin
    reset-enable: false
  web-stat-filter:
    enabled: true
    url-pattern: /*
    exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

3. 配置类

创建一个配置类来配置Druid数据源。

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }
}

4. 启动Druid监控

在你的Spring Boot应用程序中启用Druid监控。

在主类上添加注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan // 启用Druid的监控
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

5. 访问Druid监控页面

启动Spring Boot应用程序后,可以通过http://localhost:8080/druid访问Druid监控页面。默认的用户名和密码在application.yml中配置为admin

通过以上步骤,你已经成功在Spring Boot项目中配置了Druid连接池,并启用了监控功能。Druid不仅提供了强大的连接池功能,还能帮助你监控和优化数据库连接的使用情况。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,目前Spring Boot的最新版本是2.x系列,并没有Spring Boot 3。不过我可以为您介绍一下在Spring Boot 2.x如何配置Druid连接池。 在Spring Boot配置Druid连接池需要以下几个步骤: 1. 首先,在pom.xml文件添加Druid和数据库驱动的依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> <!-- 添加数据库驱动依赖,这里以MySQL为例 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> ``` 2. 在application.properties或application.yml文件配置Druid连接池的相关属性,例如: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 # Druid连接池配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.initial-size=5 spring.datasource.min-idle=5 spring.datasource.max-active=20 spring.datasource.max-wait=60000 spring.datasource.time-between-eviction-runsillis=60000 spring.datasource.min-evictable-idle-time-m=300000 spring.datasource.validation-query=SELECT 1 FROM DUAL spring.datasource.test-while-idle=true spring.datasource.test-on-borrow=false spring.datasource.test-on-return=false spring.datasource.pool-prepared-statements=true spring.datasource.max-pool-prepared-statement-per-connection-size=20 spring.datasource.filters=stat,wall,log4j .datasource.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=0 ``` 3. 在启动类上添加`@EnableTransactionManagement`注解,开启事务管理。 ```java @SpringBootApplication @EnableTransactionManagement public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 以上就是在Spring Boot配置Druid连接池的基本步骤。您可以根据自己的需求进行相应的配置调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chat2tomorrow

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值