项目场景:
在maven创建mybatis项目中,出现Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 问题
问题描述:
翻译一下报错信息,就是jdbc时区不对
//服务器时区值''�й���ʱ��'`。
//如果要利用时区支持,则必须配置服务器或JDBC驱动程序(通过“serverTimezone”配置属性)以使用更特定的时区值。
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException:
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 specific time zone value if you want to utilize time zone support.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:134)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2186)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2209)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1318)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:967)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
... 23 more
解决方案:
处理方式也很简单,修改你mybatis.xml关于jdbc的配置,在url中加一个serverTimezone=UTC即可
<dataSource type="POOLED">x
<property name="driver" value="com.mysql.jdbc.Driver"/>
//就是下面这句
<property name="url" value="jdbc:mysql://localhost:3306/x?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>