SpringBoot 2.0 启动报错 Failed to auto-configure a DataSource

梦里梦外; 2023-06-29 10:31 116阅读 0赞

springboot 2.0 启动报错 Failed to auto-configure a DataSource

原因

在pom中添加了mybatis,mysql依赖后,如果application.properties中没有配置数据库连接方面的配置,则会报错:

  1. 这是因为添加了数据库组件,所以autoconfig会去读取数据源配置,而我新建的项目还没有配置数据源,所以会导致异常出现。
  2. ***************************
  3. APPLICATION FAILED TO START
  4. ***************************
  5. Description:
  6. Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
  7. Reason: Failed to determine a suitable driver class
  8. Action:
  9. Consider the following:
  10. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  11. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

解决方法

1.在启动类中加注解

  1. 在启动类的 @EnableAutoConfiguration @SpringBootApplication 中添加
  2. exclude = {DataSourceAutoConfiguration.class},排除此类的autoconfig。启动以后就可以正常运行。

2.在配置文件application.properties中添加数据源配置,或在pon.xml去除maven依赖

发表评论

表情:
评论列表 (有 0 条评论,116人围观)

还没有评论,来说两句吧...

相关阅读