使用高版本mysql-connector-java注意事项
异常信息
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 com.ittze.mapper/UserMapper.xml
### The error may involve com.ittze.mapper.UserMapper.findAll
### The error occurred while executing a query
### 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.
原因
jar包版本过高 (如 :mysql-connector-java-6.x.x.jar版本或以上的mysql-connector-java-8.0.x.jar版本就会报此种错误)。然后,对于新版本的jar包需要 改一下驱动 和 设置一下时区。
解决方法
①改驱动:com.mysql.jdbc.Driver 改为: com.mysql.cj.jdbc.Driver
②改url路径:jdbc:mysql//localhost:3306/db1 改为:jdbc:mysql://localhost:3306/db1?serverTimezone=GMT%2B8
当然,url后面的时区改动不是定死的,还可以改其它的如:UTC(世界均衡时间),或者GMT(格林尼治时间):
jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=UTC
jdbc:mysql://localhost:3306/demo?serverTimezone=GMT
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=GMT
再或者中国时间(北京时间):
jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=GMT%2B8
作者留言(不重要的事情):BB一下改bug的时光,平时用Mybatis好好的,然后今天突然报错,一开始挺绝望的,但接着改着改着就更绝望了。But最后还是解决了。单纯记录一下。