網站首頁 編程語言 正文
failed to configure a datasource: ‘url‘ attribute is not specified and no em
作者:yetaodiao 更新時間: 2022-08-28 編程語言一. 異常現象failed to configure a datasource: 'url' attribute is not specified and no em
我在Spring Boot中關聯MySQL、Mybatis進行數據庫開發時,按照正常步驟添加了相關數據庫的依賴,也進行了必要的數據庫配置,結果在項目啟動時出現如下異常信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
看提示信息是說當前項目沒有配置DataSource相關的配置!
二. 異常原因
其實這個異常在SpringBoot中是一個比較常見的異常,一般是因為SpringBoot自動配置時,檢測到我們添加了MySQL、Oracle、Mybatis等和數據庫相關的依賴包,結果我們的配置文件中卻沒有添加數據庫相關的配置,比如:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db?characterEncoding=utf-8
username: root
password: root
三. 解決辦法failed to configure a datasource: 'url' attribute is not specified and no em
針對以上原因造成的異常,可以采用如下辦法解決:
- 在application.yml/application.properties中添加數據庫相關配置;
- 在SpringBootApplication注解中進行數據庫配置的排除,即@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})。
以上兩種解決辦法,可以解決一般的情況,但是還有一種情況比較特殊,就是當我們進行Spring Boot分模塊開發,且不同模塊中有多個application.yml或application.properties配置文件時造成的,如下圖所示:
?
我的這個案例中,我是把一個Spring Boot項目拆分成了shop-web、shop-service、shop-mapper等若干個模塊,shop-mapper模塊是數據庫操作層,里面有個application.yml配置文件進行了數據庫連接配置;shop-web模塊中也有個application.yml文件,進行了端口、程序名等的配置。我把入口類寫在shop-web模塊中,然后進行項目啟動,結果也產生了上面的異常信息。
?
造成該異常的原因,是因為我這個項目中是分模塊開發,但是這幾個模塊共同組成了一個項目,shop-web模塊依賴shop-service,shop-service模塊依賴shop-mapper,這樣就相當于一個項目中產生了2個application.yml文件,web層的application.yml文件把mapper層的application.yml配置給覆蓋掉了,所以產生了以上異常。
解決辦法有2種思路:
- 把shop-web模塊中的application.yml文件改成application.properties;
-
對不同模塊中的配置文件,以application-*.yml的形式命名,比如application-mapper.yml,application-service.yml等,然后在最頂層的shop-web模塊配置文件中,通過spring.profiles.active進行激活配置,如下圖所示:
?
-------------------------------------------------
如果是別的項目導致failed to configure a datasource: 'url' attribute is not specified and no em,
原因:項目新搭建的時候,引入了mybatis等框架,但是沒有在yml等配置文件中配置鏈接,所以導致報錯。
解決:1.刪掉mybatis等框架的引用,2.配置數據庫再鏈接。
原文鏈接:https://blog.csdn.net/yetaodiao/article/details/126562162
相關推薦
- 2022-09-24 Python?Matplotlib通過plt.subplots創建子繪圖_python
- 2022-09-25 2022react高頻面試題有哪些
- 2022-10-25 Android自定義View實現水波紋擴散效果_Android
- 2023-07-26 webpack打包優化之減少代碼體積(Tree shaking、babel)
- 2022-03-16 .NET6導入和導出EXCEL_實用技巧
- 2023-11-11 tensorflow分布式報錯:tensorflow.python.framework.errors
- 2022-04-30 C/C++編程語言中的指針(pointer)你了解嗎_C 語言
- 2023-05-07 numpy.concatenate函數用法詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支