◉◡◉ 您好,欢迎到访伊成个人站!

springBoot启动提示If you want an embedded database (H2, HSQL or Derby), please put

异常

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

说明

对于这个异常的解决方案,网上绝大部分都是说:在启动的类中的@SpringBootApplication 改为如下:

1
2
3
4
5
6
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//排除自动配置
public class ErukaServerMain {
public static void main(String[] args) {
SpringApplication.run(ErukaServerMain.class,args);
}
}

这样改没有解决实际的问题。

产生这个错误的原因是Spring Boot的自动配置,如果你没有配置DataSource就会导致下图这个错误。

那如果你很确定,比如你就是要Spring Boot + Mybatis + MySQL 整合的代码,此时就应该去检查你的配置文件中是否正确配置了数据库连接。

举个栗子

下面我们举个栗子复现这个问题,这里我使用Spring Boot + JPA + MySQL整合一个demo。

在数据库链接配置文件中,我们故意写错

1
2
3
4
spring.datasource.mysql.jdbc-url=jdbc:mysql://127.0.0.1:3306/beauty_atlas_server?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.datasource.mysql.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.mysql.username=root
spring.datasource.mysql.password=root

tips: Spring Boot + JPA 配置连接数据库 可不是用 spring.datasource.mysql.jdbc-url 这个哦,以上的这块配置都是错的,这个时候你启动就会提示

“论坛内容管理系统”

那正确的解决方法如下:

1
2
3
4
5
6
# 数据库配置 spring boot + jpa 数据库配置前缀是下面这样的
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/beauty_atlas_server?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

由上可见,Spirng Boot 和不同的持久层整合这些配置都是有所区别的,在整合的过程中一定要慎重。
Spring Boot + JPA + MySQL 整合中还得配置DataSource,把它注入到Spring中接口,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@ComponentScan
@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class DbConfig {

private String url;
private String username;
private String password;

@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

重新启动,项目无问题了。


The end.

支付宝打赏 微信打赏