在mybatis连接到mysql的时候出现报错,错误信息如下:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
环境:此时我的mysql的版本为5.7
在经过一系列的问题查找后,最后将问题锁定在mysql的版本和springboot中driver版本不兼容>上。
我的解决方案:
1.可以尝试将application.properties中的配置项useSSL=true修改为useSSL=false(没有解决)
//application.porperties的配置
spring.datasource.url=jdbc:mysql://localhost:3306/study?
serverTimezone=UTC&useUnicode=true
&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2.安装与之匹配的mysql版本
- 卸载旧的版本
- 直接安装新的版本,不卸载旧的
我采用的是第二种,没有卸载旧的版本,我安装的版本如下:
在进行安装的时候可以参考以下我参考过的博文:
Could not start the service MySQL解决方法
安装包链接我挂在下面了,当然也可以自己去找:
链接:https://pan.baidu.com/s/1qbbJ2w1u6nJMkcY3pbeL3A
提取码:6666
在安装完新的MySQL之后记得把环境变量修改一下
5.7版本
系统变量 MYSQL_HOME C:\Program Files\MySQL\MySQL Server 5.7
系统变量 Path C:\Program Files\MySQL\MySQL Server 5.7\bin
5.5版本
系统变量 MYSQL_HOME E:\Program Files\MySQL\MySQL Server 5.5
系统变量 Path E:\Program Files\MySQL\MySQL Server 5.5\bin
再检查一下是否生效
//cmd下输入
mysql --version
如果没有在出现之前的错误了,但是报的是其他的错误的或者没报了就表示mybatis已经与mysql建立好了连接了。
相关的依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
我的项目结构如下:
注:如果自动注入@AutoWired出现报红,记得在接口上加上@Mapper,如下
@Mapper
public interface StudentMapper {
}