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

发布时间:2021-08-02 10:54:00 作者:chen
来源:亿速云 阅读:167

本篇内容主要讲解“如何解决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})

到此,相信大家对“如何解决Spring boot数据源未配置异常”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. spring boot的运行原理
  2. Spring Boot集成MyBatis的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:PHP怎么判断字符串是不是有某个特定词

下一篇:nodejs如何使用redis作为缓存介质实现封装缓存类

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》