1. 程式人生 > 實用技巧 >關於“Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.”

關於“Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.”

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).

錯誤原因:

顯然,根據錯誤原因可知找不到資料來源配置。但是由於依賴方提供的API依賴中引用了一些多餘的依賴觸發了該自動化配置的載入,詳情檢視:

https://blog.csdn.net/dyc87112/article/details/73739535,導致資料來源配置自動啟動生效。

這裡是由於依賴了:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

解決方案:

1.不配置資料來源的情況下:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class})

詳細請參考:https://zhuanlan.zhihu.com/p/96508798

2.配置資料來源:本文資料來源配置在yml檔案中:

spring:
  datasource:
    url: jdbc:mysql://xxxxxxxxxx
    username: xxx
    password: xxx
    driver-class-name: com.mysql.jdbc.Driver

注意:pom檔案中package型別不能為pom,否則專案編譯後target中沒有yml配置檔案,同理打包後jar包中也沒有yml配置檔案。

如下位置:

 <groupId>com.xxx</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>demo</name>
<!--<package>pom</package>--> <description>Demo project for Spring Boot</description>

以上!