Spring boot 数据源未配置异常的解决

网友投稿 696 2022-12-22

Spring boot 数据源未配置异常的解决

Spring boot 数据源未配置异常的解决

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.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启动提示缺少数据源

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})

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:车联网车载终端无法使用(车载系统无法联网)
下一篇:前端框架应用(前端框架应用实训心得)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~