「微服务」你会用SpringBoot+MyBatis连接Mysql8.0吗?

#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/rwa?

「微服务」你会用SpringBoot+MyBatis连接Mysql8.0吗?

美女镇楼 吃喝不愁

1.引入项目依赖:

<!– 数据源 –>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.1.0</version>

</dependency>

<!– mysql –>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.11</version>

</dependency>

<!–mybatis–>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.1</version>

</dependency>

2.配置数据源相关,使用阿里巴巴Druid数据源

### MySQL Connector/J 5.x (旧版连接)

#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/rwa?useUnicode=true&characterEncoding=utf8

### MySQL Connector/J 6.x之后 (新版连接)

spring.datasource.url=jdbc:mysql:///rwa?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false

#说明: 新版驱动url默认地址为127.0.0.1:3306,所以访问本机mysql数据库地址可以用 /// 表示

#spring.datasource.url=jdbc:mysql://localhost:3306/rwa?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false

spring.datasource.username=root

spring.datasource.password=qzw123

#spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.druid.initial-size=1

spring.datasource.druid.min-idle=1

spring.datasource.druid.max-active=20

spring.datasource.druid.test-on-borrow=true

spring.datasource.druid.stat-view-servlet.allow=true

注意:新版mysql驱动的url必须设置时区,即serverTimezone=UTC,否则会报如下错误:

「微服务」你会用SpringBoot+MyBatis连接Mysql8.0吗?

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.

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:148) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:217) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:142) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1466) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1529) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2178) ~[druid-1.1.0.jar:1.1.0]

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 specifc time zone value if you want to utilize time zone support.

at sun.reflect.GeneratedConstructorAccessor85.newInstance(Unknown Source) ~[na:na]

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_91]

at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_91]

at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993) ~[mysql-connector-java-8.0.11.jar:8.0.11]

at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852) ~[mysql-connector-java-8.0.11.jar:8.0.11]

… 9 common frames omitted

加上时区参数serverTimezone=UTC

「微服务」你会用SpringBoot+MyBatis连接Mysql8.0吗?

项目正常启动!

————————————————

版权声明:本文为CSDN博主「弗朗明戈S」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_20788055/article/details/80380099

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/10377.html

(0)
上一篇 2023年 4月 22日 下午11:58
下一篇 2023年 4月 22日 下午11:58

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信