Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection
springboot与mybatis整合需要注意几点
如果引入的是低版本的数据库连接驱动
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version> <!--低版本数据库连接驱动-->
</dependency>
<!--springboot与JDBC整合包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot与mybatis的整合包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
application.properties配置如下:
#端口配置
server.port = 8080
#数据库配置
spring.datasource.url = jdbc:mysql://localhost:3306/数据库名?useSSL=false
#用户名
spring.datasource.username =
#密码
spring.datasource.password =
#驱动
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
如果引入的是高版本的数据库连接驱动
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId><!--不说明版本默认是最新的-->
</dependency>
<!--springboot与JDBC整合包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot与mybatis的整合包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
application.properties配置需要修改一下,如下:
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/数据库名?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
如果不改动,当我们启动项目的时候并不会报错,但访问数据的时候页面就会出错:

具体报错信息:
There was an unexpected error (type=Internal Server Error, status=500).
nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection;
nested exception is 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/example/app/mapper/UserRepository.java (best guess) ### The error may involve com.example.app.mapper.UserRepository.
getAllUser ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is 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.
1582

被折叠的 条评论
为什么被折叠?



