最近在寫一個項目的時候,用了maven倉庫裏面較新的mysql的JDBC驅動,版本是6.0.6,Mybatis的全局配置是這麼寫的:java
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default=""> <environment id=""> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="sqlmap/User.xml"/> </mappers> </configuration>
可是卻發現報錯了,錯誤緣由是:mysql
Error querying database. Cause: 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.sql
這是由於在訪問數據庫時沒法識別時區(我選擇死亡(╬▔皿▔)凸),因此咱們須要把JDBC的url值改成這樣:數據庫
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC"/>
這裏要注意了,咱們編譯一下項目發現出現了這樣的錯誤:mybatis
Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 119; 對實體 "serverTimezone" 的引用必須以 ';' 分隔符結尾。app
這裏實際上是由於xml把&做爲一個特殊符號處理了(我選擇再次死亡(╬▔皿▔)凸),因此咱們須要把&替換爲&這樣就不會報錯了。maven
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC"/>
固然咱們也能夠直接修改mysql的時區,打開mysql,輸入set global time_zone='+8:00';ide
解決了這個問題以後,咱們繼續回到配置文件,咱們再編譯一下項目,此次錯誤卻是沒有了,可是還有一些警告,雖然咱們通常忽略警告,可是看起來仍是挺不舒服的,因此解決一下吧。其中一個警告是這樣的:ui
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.url
這是由於mysql的JDBC驅動使用了新的包名,因此咱們須要將driver的值改成com.mysql.cj.jdbc.Driver
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
還有一個警告是這樣的:
Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
由於新版本的MySQL要求是否進行ssl鏈接,因此咱們須要設置useSSL=false或者useSSL=true。
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false"/>
接下來,咱們再編譯一遍項目,總算0 error, 0 warning了。咱們也能看到正確結果了。