springBoot 启动报错: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

发布时间 2023-09-20 18:45:36作者: 爱新觉罗LQ

原因

其实这个异常在SpringBoot中是一个比较常见的异常,一般是因为SpringBoot自动配置时,检测到我们添加了MySQL、Oracle、Mybatis等和数据库相关的依赖包,结果我们的配置文件中却没有添加数据库相关的配置,比如:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db?characterEncoding=utf-8
    username: root
    password: root

解决方法

1. 在 yml 中添加相关配置

2. 在SpringBootApplication注解中进行数据库配置的排除,即@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})。