关于mysql-connector-java(JDBC驱动)的⼀些坑最近在写⼀个项⽬的时候,⽤了maven仓库⾥⾯较新的mysql的JDBC驱动,版本是6.0.6,Mybatis的全局配置是这么写的:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="">
<environment id="">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="sql.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>
mysql下载后为啥localhost打不开<mapper resource="l"/>
</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: l.sax.SAXParseException; lineNumber: 11; columnNumber: 119; 对实体
"serverTimezone" 的引⽤必须以 ';' 分隔符结尾。
这⾥其实是因为xml把&作为⼀个特殊符号处理了(我选择再次死亡(╬▔⽫▔)凸),所以我们需要把&替换为&;这样就不会报错了。
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC"/>
当然我们也可以直接修改mysql的时区,打开mysql,输⼊set global time_zone='+8:00';
解决了这个问题之后,我们继续回到配置⽂件,我们再编译⼀下项⽬,这次错误倒是没有了,但是还有⼀些警告,虽然我们⼀般忽略警告,但是看起来还是挺不舒服的,所以解决⼀下吧。其中⼀个警告是这样的:
Loading class `sql.jdbc.Driver'. This is deprecated. The new driver class is `sql.cj.jdbc.Driver'. The driver is
automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
这是因为mysql的JDBC驱动使⽤了新的包名,所以我们需要将driver的值改为sql.cj.jdbc.Driver
<property name="driver" value="sql.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了。我们也能看到正确结果了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论