国产精品成人网站,日韩视频二区,亚洲成人手机电影,怡红院国产

行業(yè)產品

  • 行業(yè)產品

安徽思成儀器技術有限公司


當前位置:安徽思成儀器技術有限公司>技術文章>failed to configure a datasource: ‘url‘ attribute is not specified and no em
技術文章

failed to configure a datasource: ‘url‘ attribute is not specified and no em

閱讀:16發(fā)布時間:2024-11-23

一. 異?,F(xiàn)象failed to configure a datasource: 'url' attribute is not specified and no em
我在Spring Boot中關聯(lián)MySQL、Mybatis進行數(shù)據(jù)庫開發(fā)時,按照正常步驟添加了相關數(shù)據(jù)庫的依賴,也進行了必要的數(shù)據(jù)庫配置,結果在項目啟動時出現(xiàn)如下異常信息:
 
***************************
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等和數(shù)據(jù)庫相關的依賴包,結果我們的配置文件中卻沒有添加數(shù)據(jù)庫相關的配置,比如:
 
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中添加數(shù)據(jù)庫相關配置;
在SpringBootApplication注解中進行數(shù)據(jù)庫配置的排除,即@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})。
 
以上兩種解決辦法,可以解決一般的情況,但是還有一種情況比較特殊,就是當我們進行Spring Boot分模塊開發(fā),且不同模塊中有多個application.yml或application.properties配置文件時造成的,如下圖所示:
 
 
我的這個案例中,我是把一個Spring Boot項目拆分成了shop-web、shop-service、shop-mapper等若干個模塊,shop-mapper模塊是數(shù)據(jù)庫操作層,里面有個application.yml配置文件進行了數(shù)據(jù)庫連接配置;shop-web模塊中也有個application.yml文件,進行了端口、程序名等的配置。我把入口類寫在shop-web模塊中,然后進行項目啟動,結果也產生了上面的異常信息。
 
 
 
造成該異常的原因,是因為我這個項目中是分模塊開發(fā),但是這幾個模塊共同組成了一個項目,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.配置數(shù)據(jù)庫再鏈接。
————————————————

問題原因: Mybatis沒有找到合適的加載類,其實是大部分spring - datasource - url沒有加載成功,分析原因如下所示.

  1. DataSourceAutoConfiguration會自動加載.

  2. 沒有配置spring - datasource - url 屬性.

  3. spring - datasource - url 配置的地址格式有問題.

  4. 配置 spring - datasource - url的文件沒有加載.



方案一 (解決原因1)

排除此類的autoconfig。啟動以后就可以正常運行。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
方案二 (解決原因2)

在application.properties/或者application.yml文件中沒有添加數(shù)據(jù)庫配置信息.

spring:  datasource:    url: jdbc:mysql://localhost:3306/read_data?useUnicode=true&characterEncoding=UTF-8&useSSL=false    username: root    password: 123456    driver-class-name: com.mysql.jdbc.Driver
方案三 (解決原因3)

在spring xml配置文件中引用了數(shù)據(jù)庫地址 所以需要對:等進行轉義處理.但是在application.properties/或者application.yml文件并不需要轉義,錯誤和正確方法寫在下面了.

//錯誤示例spring.datasource.url = jdbc:mysql/://192.168.0.20/:1504/f_me?setUnicode=true&characterEncoding=utf8
//正確示例spring.datasource.url = jdbc:mysql://192.168.0.20:1504/f_me?setUnicode=true&characterEncoding=utf8
方案四 (解決原因4)

yml或者properties文件沒有被掃描到,需要在pom文件中添加如下.來保證文件都能正常被掃描到并且加載成功.

<resources>    <resource>        <directory>src/main/javadirectory>        <includes>            <include>**/*.ymlinclude>            <include>**/*.propertiesinclude>            <include>**/*.xmlinclude>        includes>        <filtering>falsefiltering>    resource>    <resource>        <directory>src/main/resourcesdirectory>        <includes>            <include>**/*.ymlinclude>            <include>**/*.propertiesinclude>            <include>**/*.xmlinclude>        includes>        <filtering>falsefiltering>    resource>resources>


儀表網 設計制作,未經允許翻錄必究 .? ? ? Copyright(C)?2021 http://caturday.cn,All rights reserved.

以上信息由企業(yè)自行提供,信息內容的真實性、準確性和合法性由相關企業(yè)負責,儀表網對此不承擔任何保證責任。 溫馨提示:為規(guī)避購買風險,建議您在購買產品前務必確認供應商資質及產品質量。

會員登錄

×

請輸入賬號

請輸入密碼

=

請輸驗證碼

收藏該商鋪

登錄 后再收藏

提示

您的留言已提交成功!我們將在第一時間回復您~