SpringBoot整合數(shù)據(jù)庫連接池Druid的錯誤詳解
前言
今天使用SpringBoot整合數(shù)據(jù)庫連接池Druid的錯誤總結(jié)的時候,遇到了幾個問題,都已經(jīng)解決,在這里分享給大家
發(fā)現(xiàn)和解決問題
在這些問題中,我會通過復(fù)制關(guān)鍵字和截圖的方式,幫助大家確實(shí)錯誤是否一致
問題1
如圖所示,控制臺顯示貌似已經(jīng)成功了,但是我們可以發(fā)現(xiàn),其實(shí)在啟動成功之后,因?yàn)橐粋€錯誤,導(dǎo)致系統(tǒng)停止了

想要解決這個問題,可以再加一個jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>這時候我們再次啟動,解決問題

問題2
關(guān)鍵日志:
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘dataSource’: Unsatisfied dependency expressed through field ‘basicProperties’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties’: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties’: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType

解決這個問題,也是需要一個jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>再次啟動解決問題
需要注意的地方
配置數(shù)據(jù)庫的相關(guān)信息的時候
注意用戶名和密碼,以及url,尤其是url,不同的庫以及版本不同,都會導(dǎo)致url出錯
還有就是jar包沖突,缺少jar包等
相關(guān)代碼
這里我把我的pom相關(guān)依賴和配置文件分享給大家
<dependencies>
<!--druid數(shù)據(jù)庫連接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT&allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=utf8 spring.datasource.druid.username=root spring.datasource.druid.password=root spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.druid.initialSize=10 spring.datasource.druid.maxActive=20 spring.datasource.druid.maxWait=60000 spring.datasource.druid.minIdle=1 spring.datasource.druid.timeBetweenEvictionRunsMillis=60000 spring.datasource.druid.minEvictableIdleTimeMillis=300000 spring.datasource.druid.testWhileIdle=true spring.datasource.druid.testOnBorrow=true spring.datasource.druid.testOnReturn=false spring.datasource.druid.poolPreparedStatements=true spring.datasource.druid.maxOpenPreparedStatements=20 spring.datasource.druid.validationQuery=SELECT 1 spring.datasource.druid.validation-query-timeout=500 spring.datasource.druid.filters=stat spring.datasource.druid.stat-view-servlet.enabled=true # 訪問地址規(guī)則 spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* # 是否允許清空統(tǒng)計(jì)數(shù)據(jù) spring.datasource.druid.stat-view-servlet.reset-enable=true # 監(jiān)控頁面的登錄賬戶 spring.datasource.druid.stat-view-servlet.login-username=admin # 監(jiān)控頁面的登錄密碼 spring.datasource.druid.stat-view-servlet.login-password=admin
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用Spring Batch進(jìn)行批處理任務(wù)管理
本文介紹了如何配置Spring Batch、如何創(chuàng)建批處理任務(wù),以及如何讀取和寫入數(shù)據(jù),希望通過本文的介紹,你能更好地理解和使用Spring Batch來管理批處理任務(wù),感興趣的朋友跟隨小編一起看看吧2024-08-08
關(guān)于MybatisPlus配置雙數(shù)據(jù)庫驅(qū)動連接數(shù)據(jù)庫問題
這篇文章主要介紹了MybatisPlus配置雙數(shù)據(jù)庫驅(qū)動連接數(shù)據(jù)庫的具體實(shí)現(xiàn),具體的業(yè)務(wù)邏輯,在service層的類或者方法上面添加@DataSource注解來指定該業(yè)務(wù)需要用到的數(shù)據(jù)源,需要的朋友可以參考下2022-01-01
解決cmd運(yùn)行java程序“找不到文件”提示的方案
在本篇文章里小編給大家分享的是關(guān)于解決cmd運(yùn)行java程序“找不到文件”提示的方案,有需要的朋友們可以參考下。2020-02-02
詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶端負(fù)載均衡
這篇文章主要為大家介紹了詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶端負(fù)載均衡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
MyBatis-Plus多數(shù)據(jù)源配置與讀寫分離全過程
文章介紹了如何在SpringBoot中使用MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)庫操作,包括純粹多庫和讀寫分離的配置方法,重點(diǎn)演示了多數(shù)據(jù)源的設(shè)置與使用2025-09-09
IntelliJ IDEA 設(shè)置數(shù)據(jù)庫連接全局共享的步驟
在日常的軟件開發(fā)工作中,我們經(jīng)常會遇到需要在多個項(xiàng)目之間共享同一個數(shù)據(jù)庫連接的情況,默認(rèn)情況下,IntelliJ IDEA 中的數(shù)據(jù)庫連接配置是針對每個項(xiàng)目單獨(dú)存儲的,幸運(yùn)的是,IntelliJ IDEA 提供了一種方法來將數(shù)據(jù)庫連接配置設(shè)置為全局共享,從而簡化這一過程2024-10-10

