mybatis 对mysql8 的配置的一些坑

1. 错误描述

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### 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.
### The error may exist in mapper/UserMapper.xml
### The error may involve com.day01_1.pojo.selectbyid
### The error occurred while executing a query
### Cause: java.sql.SQLException

2. 错误原因

(1)服务器时区值不可识别,应该在MySQL连接字符串后面加上属性:serverTimezone。
JDBC连接Mysql8时应该使用com.mysql.cj.jdbc.Driver驱动, 并且需要指定时区serverTimezone。

<dataSource type="POOLED">
    <property name="driver" value="com.mysql.cj.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" />
    <property name="username" value="root" />
    <property name="password" value="lrk" />
</dataSource>

(2)在设定时区的时候,如果设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Hongkong或者GMT+8。

<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=Hongkong" />
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8" />

3. 其他常见的错误及解决方案

(1)有些版本的MySQL如果未设置显式选项,则必须默认建立SSL连接,为了符合不使用SSL的现有应用程序,您可以将verifyServerCertificate属性设置为false,您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并提供用于服务器证书验证的信任库

解决方案:在MySQL连接数据库字符串后加上参数&useSSL=false。
jdbc:mysql://localhost:3306/mail_server?useSSL=false&amp;serverTimezone=GMT%2B8
<!--xml文件的&需要转义成&amp;,如果是字符串配置则不需要,直接用&-->

(2)xml中配置错误,例如需要转义的字符没有转义,如下:

jdbc:mysql://localhost:3306/mail_server?useSSL=false&serverTimezone=GMT%2B8
解决方案:将&serverTimezone=UTC替换为&serverTimezone=UTC,如下:
jdbc:mysql://localhost:3306/mail_server?useSSL=false&amp;serverTimezone=GMT%2B8
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值