您的位置:首頁技術文章
文章詳情頁

Spring boot 數據源未配置異常的解決

瀏覽:2日期:2023-06-28 08:15:35
Spring boot 數據源未配置異常問題

在使Springboot自動生成的項目框架時如果選擇了數據源,比如選擇了mysql,生成項目之后,啟動會報一下異常:

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

問題分析

導致此問題的原因為,springboot生成的項目啟動時會自動注入數據源。而此時在配置文件中并沒有配置數據源信息,因此會拋出異常。

解決方案

(1)如果暫時不需要數據源,可將pom文件中的mysql和mybatis(或其他數據源框架)注釋掉,即可正常啟動。

(2)在@SpringBootApplication中排除其注入

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

(3)提供數據源的配置或其他數據源配置,此處提供默認配置示例,在application.properties文件中添加以下配置項:

# 主數據源,默認的#spring.datasource.type=com.zaxxer.hikari.HikariDataSourcespring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=rootspringboot啟動提示缺少數據源

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently

正解:

因為spring boot只要你在pom中引入了mybatis-spring-boot-starter 他就會默認需要加載數據庫相關的配置

可以加上

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
国产综合久久一区二区三区