关于mysql-connector-java(JDBC驱动)的一些坑

最近在写一个项目的时候,用了maven仓库里面较新的mysql的JDBC驱动,版本是6.0.6,Mybatis的全局配置是这么写的:

<?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>

但是却发现报错了,错误原因是:

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.

这是因为在访问数据库时无法识别时区(我选择死亡(╬▔皿▔)凸),所以我们需要把JDBC的url值改为这样:

<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC"/>

这里要注意了,我们编译一下项目发现出现了这样的错误:

 Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 119; 对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。

这里其实是因为xml把&作为一个特殊符号处理了(我选择再次死亡(╬▔皿▔)凸),所以我们需要把&替换为&amp;这样就不会报错了。

<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&amp;serverTimezone=UTC"/>

当然我们也可以直接修改mysql的时区,打开mysql,输入set global time_zone='+8:00';

解决了这个问题之后,我们继续回到配置文件,我们再编译一下项目,这次错误倒是没有了,但是还有一些警告,虽然我们一般忽略警告,但是看起来还是挺不舒服的,所以解决一下吧。其中一个警告是这样的:

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.

这是因为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&amp;serverTimezone=UTC&amp;useSSL=false"/>

接下来,我们再编译一遍项目,总算0 error, 0 warning了。我们也能看到正确结果了。

 

posted @ 2017-07-28 17:18  Silence.Sky  阅读(90038)  评论(2编辑  收藏  举报