集成Spring-boot和Spring-data-jpa兩個框架進行開發時,在屬性配置文件 application.properties 中有這樣一段
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
啓動項目時報錯:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
百度之後,得知這是由於數據庫時區沒有正確設置導致的。在數據庫url中加入serverTimeZone=GMT%2B8
問題得到解決。(報錯信息中的亂碼不知道怎麼來的,也不知道什麼意思,似乎也不影響問題的處理)
再啓動項目可以啓動,不過還有兩個警告信息:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'
直接按照提示信息,將 driver-class 的值改成 com.mysql.cj.jdbc.Driver 即可。
WARN: Establishing SSL connection without server's identity verification is not recommended
這個警告信息時由於Mysql數據庫在5.7默認開啓SSL加密鏈接,在數據庫url中增加如下字段就可消除這段警告信息
useSSL=false
所以,最終的配置修改如下:
spring.datasource.url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver