Spring boot 数据源缺失异常解决方案
程序员文章站
2022-05-24 16:12:26
...
问题
在使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.aspricn.HikariDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#必要时加上字符编码
spring.datasource.url=jdbc:mysql://localhost:3306/test$charset=utf8
spring.datasource.username=root
spring.datasource.password=root
推荐阅读
-
spring boot请求异常处理并返回对应的html页面
-
只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常
-
Spring boot 集成 Druid 数据源过程详解
-
spring boot springjpa 支持多个数据源的实例代码
-
spring boot 2 全局统一返回RESTful风格数据、统一异常处理
-
spring boot从redis取缓存发生java.lang.ClassCastException异常
-
Spring Boot项目添加外部Jar包以及配置多数据源的完整步骤
-
Spring boot如何快速的配置多个Redis数据源
-
spring boot多数据源动态切换代码实例
-
spring-boot-2.0.3之quartz集成,数据源问题,源码探究