最近在写一个项目的时候,用了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了。咱们也能看到正确结果了。