報錯緣由以下:com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone..java
產生這個問題的緣由主要在於:MySQL JDBC驅動程序的5.1.33版本與UTC時區配合使用,必須在鏈接字符串中明確指定serverTimezone。mysql
當時這個問題出現於springboot2.0.0及以上版本結合mybatis2.0.0版本上,以下依賴包:spring
第一種:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
上面若是配置成 spring.datasource.url=jdbc:mysql://localhost:3306/test就會報此錯誤。
第二種:
<!--繼承springboot的父級依賴包-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!--加載mybatis整合springboot的起步依賴包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql的jdbc依賴包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
這種狀況若是配置成 spring.datasource.url=jdbc:mysql://localhost:3306/test就不會報錯。
所以爲了安全起見,最好統一加上以下配置:
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true