数据源配置异常

Posted 天河一粟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据源配置异常相关的知识,希望对你有一定的参考价值。

先上异常信息

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-31 18:53:44.137 ERROR 15044 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

针对如上异常原因有以下几点

  1. 数据源配置不对,需要在application.properties增加如下数据源,注意二选一

    # mysql数据库连接 spring boot 2.0(内置jdbc5驱动)
    #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #spring.datasource.url=jdbc:mysql://localhost:3306/testdb?characterEncoding=utf-8&useSSL=false
    #spring.datasource.username=root
    #spring.datasource.password=root
    # mysql数据库连接 spring boot 2.1及以上(内置jdbc8驱动)
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/testdb?serverTimezone=GMT%2B8
    spring.datasource.username=root
    spring.datasource.password=root
    
  2. 在编译后的target目录中配置数据源的application.properties文件位置不对,把资源文件放到classes目录下就好啦

  3. 使用--spring.config.location指定的配置文件的位置
    ① 命令方式
    java -jar xxx.jar --spring.config.location=D:\\a\\b\\application.properties,D:\\a\\b\\logback-spring.xml

    ② idea开发工具参数形式

以上是关于数据源配置异常的主要内容,如果未能解决你的问题,请参考以下文章

异常和TCP通讯

如何绕过将数据模型传递给片段参数以避免事务太大异常?

出现异常:片段已激活

片段中的Android致命异常

mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段

mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段