沈樵综合在线,一区二区三区乱码,免费资源一区二区三区,91超碰碰,av极品一线天,日韩免费高清一区二区,国产蜜尤福利在线,嗯啊91在线观看,精品一区二三

行業(yè)產(chǎn)品

  • 行業(yè)產(chǎn)品

安徽思成儀器技術(shù)有限公司


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

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

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

一. 異?,F(xiàn)象failed to configure a datasource: 'url' attribute is not specified and no em
我在Spring Boot中關(guān)聯(lián)MySQL、Mybatis進行數(shù)據(jù)庫開發(fā)時,按照正常步驟添加了相關(guān)數(shù)據(jù)庫的依賴,也進行了必要的數(shù)據(jù)庫配置,結(jié)果在項目啟動時出現(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
 
看提示信息是說當(dāng)前項目沒有配置DataSource相關(guān)的配置!
 
二. 異常原因
其實這個異常在SpringBoot中是一個比較常見的異常,一般是因為SpringBoot自動配置時,檢測到我們添加了MySQL、Oracle、Mybatis等和數(shù)據(jù)庫相關(guān)的依賴包,結(jié)果我們的配置文件中卻沒有添加數(shù)據(jù)庫相關(guān)的配置,比如:
 
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ù)庫相關(guān)配置;
在SpringBootApplication注解中進行數(shù)據(jù)庫配置的排除,即@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})。
 
以上兩種解決辦法,可以解決一般的情況,但是還有一種情況比較特殊,就是當(dāng)我們進行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模塊中,然后進行項目啟動,結(jié)果也產(chǎn)生了上面的異常信息。
 
 
 
造成該異常的原因,是因為我這個項目中是分模塊開發(fā),但是這幾個模塊共同組成了一個項目,shop-web模塊依賴shop-service,shop-service模塊依賴shop-mapper,這樣就相當(dāng)于一個項目中產(chǎn)生了2個application.yml文件,web層的application.yml文件把mapper層的application.yml配置給覆蓋掉了,所以產(chǎn)生了以上異常。
 
解決辦法有2種思路:
 
把shop-web模塊中的application.yml文件改成application.properties;
對不同模塊中的配置文件,以application-*.yml的形式命名,比如application-mapper.yml,application-service.yml等,然后在最頂層的shop-web模塊配置文件中,通過spring.profiles.active進行激活配置,如下圖所示:
 
 
-------------------------------------------------
 
如果是別的項目導(dǎo)致failed to configure a datasource: 'url' attribute is not specified and no em,
 
原因:項目新搭建的時候,引入了mybatis等框架,但是沒有在yml等配置文件中配置鏈接,所以導(dǎo)致報錯。
 
解決: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ù)庫地址 所以需要對:等進行轉(zhuǎn)義處理.但是在application.properties/或者application.yml文件并不需要轉(zhuǎn)義,錯誤和正確方法寫在下面了.

//錯誤示例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>


儀表網(wǎng) 設(shè)計制作,未經(jīng)允許翻錄必究 .? ? ? Copyright(C)?2021 http://m.liuyangwu.com,All rights reserved.

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

會員登錄

×

請輸入賬號

請輸入密碼

=

請輸驗證碼

收藏該商鋪

登錄 后再收藏

提示

您的留言已提交成功!我們將在第一時間回復(fù)您~
遂溪县| 务川| 怀来县| 金堂县| 长岭县| 巍山| 尚志市| 岑巩县| 黄骅市| 大兴区| 台南市| 海晏县| 清涧县| 宁城县| 平阴县| 尚志市| 江油市| 阿合奇县| 慈溪市| 曲周县| 聊城市| 马关县| 海盐县| 丁青县| 广西| 子洲县| 六盘水市| 张北县| 东宁县| 万载县| 临沂市| 丹巴县| 浪卡子县| 革吉县| 芜湖县| 昌黎县| 隆德县| 清丰县| 右玉县| 儋州市| 紫云|