問題
在使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})
- 1
(3)提供數據源的配置或其他數據源配置,此處提供默認配置示例,在application.properties文件中添加以下配置項:
# 主數據源,默認的 #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
引入依賴:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>