spring boot填坑(一)

问题1:Failed to configure a DataSource.

报错信息节选如下所示:

***************************
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).

出现这个问题的原因,大部分是由于spring - datasource - url没有加载成功,如下所示:

  1. DataSourceAutoConfiguration会自动加载

  2. 没有配置spring - datasource - url 属性

  3. spring - datasource - url 配置的地址格式有问题

  4. 配置 spring - datasource - url的文件没有加载

而导致Mybatis没有找到合适的加载类。可以尝试通过如下4种方式进行解决:

    1. 尝试排除类的autoconfig

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

    2.确认spring - datasource - url 属性的配置

    在application.properties或application.xml文件种进行了spring-datasource-url的有效配置。

    application.xml文件,mysql-connector-java 6.0.x 及以上版本配置

spring:
  datasource:
    url:jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

     application.properties文件,mysql-connector-java 6.0.x 及以上版本配置

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456

    3. 确认spring - datasource - url 配置的地址格式没有问题

   校对2中的url地址是否有效,是否正确,这两个文件中书写url时均不需要转义字符。

   4. 确认配置 spring - datasource - url的文件有效加载

   在pom.xml文件中加入类似如下所示的代码,确保配置了spring - datasource - url的文件有效加载

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <!--引入所需环境的配置文件-->
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>
</build>

问题2:时区问题

spring boot项目常用的时区问题解决方式,有两个。

1. 在application.properties或application.xml文件中进行如下配置:

spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

2. 在spring - datasource - url(mysql-connector-java 6.0.x 及以上版本)配置时,指定时区

spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章