溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決Spring boot數(shù)據(jù)源未配置異常

發(fā)布時間:2021-08-02 10:54:00 來源:億速云 閱讀:170 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“如何解決Spring boot數(shù)據(jù)源未配置異?!保信d趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“如何解決Spring boot數(shù)據(jù)源未配置異?!卑?

Spring boot 數(shù)據(jù)源未配置異常

問題

在使Springboot自動生成的項目框架時如果選擇了數(shù)據(jù)源,比如選擇了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).

問題分析

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

解決方案

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

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

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

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

# 主數(shù)據(jù)源,默認(rèn)的
#spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root

springboot啟動提示缺少數(shù)據(jù)源

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 他就會默認(rèn)需要加載數(shù)據(jù)庫相關(guān)的配置

可以加上

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

到此,相信大家對“如何解決Spring boot數(shù)據(jù)源未配置異常”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI