在 Spring Boot 中,如果你遇到了 DruidDataSource 的报错,可以按照以下步骤进行解决:

  1. 确保你的项目中已经引入了 Druid 的相关依赖。你可以在 pom.xml 文件中添加以下依赖:
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>
  1. 检查你的配置文件,确保 DruidDataSource 的相关配置正确。你可以在 application.propertiesapplication.yml 中添加以下配置:
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=username
spring.datasource.password=password

# 连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  1. 如果你使用的是 Spring Boot 2.x 版本,可以将以上配置中的 spring.datasource 改为 spring.datasource.druid,例如:
# 数据库连接配置
spring.datasource.druid.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.druid.username=username
spring.datasource.druid.password=password

# 连接池配置
spring.datasource.druid.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,wall,log4j
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  1. 确保你的数据库驱动程序已正确引入。你可以在 pom.xml 文件中添加以下依赖:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  1. 重启你的应用程序,看是否能够成功连接到数据库。

如果你仍然遇到问题,请提供完整的错误信息和相关的配置文件内容,以便我们更好地帮助你解决问题。

DruidDataSource Spring Boot 报错解决方案

原文地址: https://www.cveoy.top/t/topic/bIiP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录