### Error querying database. Cause: com.mysql.cj.jdbc.exceptions.CommunicationsExcepmybatis

问题来源:

今天在学习mybatis时,遇到的一个问题

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
### The error may exist in com/itheima/mapper/CustomerMapper.xml
### The error may involve com.itheima.mapper.CustomerMapper.findCustomerByid
### The error occurred while executing a query
### Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

解决思路:

把Communications link failure翻译成中文为:通信链路故障
查询一番后,是数据库连接源配置的问题。
有的说是jdbc:mysql://localhost:3306/mybatis后面应该跟上参数,加参数详细说明

<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>

但是我加上这些参数后依然是报一样的错。此时我开始怀疑是我用的mybatis4.3.2和mysql8.0.28不兼容导致的。于是我尝试了以下办法:
1.更换最新版本的 mybatis jar包,该方法无效。
2.尝试更换mysql版本为5.1.40,但我的Mac笔记本始终装不上5.1.40,只好放弃,该方法无效。(以为我参考的java书籍用的是mysql5.1.40,但我用的是8.0.28,所以我以为是版本不兼容问题)
3.折腾了半天装不上5.1.40所以我只好又重新安装了mysql8.0.28;
接着继续开始各种查询、、、、、、

最终的解决办法:

这是我的测试方法,

public class MybatisTest {
	@Test
	public void findCustomerByIdTest() throws Exception {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);	
		SqlSession sqlSession = sqlSessionFactory.openSession();
		Customer customer = sqlSession.selectOne("com.itheima.mapper.CustomerMapper.findCustomerByid",1);
		System.out.print(customer.toString());
		sqlSession.close();	
	}

}
上面的代码执行就报错,按照下面这样注释掉关于数据库查询的代码后,方法执行不报错,这说明除了数据库连接,代码其他地方没有问题
public class MybatisTest {
	@Test
	public void findCustomerByIdTest() throws Exception {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);	
		SqlSession sqlSession = sqlSessionFactory.openSession();
		把跟数据连接有关代码注释掉
		//Customer customer = sqlSession.selectOne("com.itheima.mapper.CustomerMapper.findCustomerByid",1);
		//System.out.print(customer.toString());
		sqlSession.close();	
	}

}

问题还是出在数据库连接上,继续检查数据库连接配置,结果发现是localhost少写了一个L,写成了locahost。

<property name="url" value="jdbc:mysql://locahost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>

加上L后,再去执行测试方法,运行成功,问题解决。
所以问题就是localhost少写了L写成了locahost、、、、、、
出现这个问题,
还是要仔细检查数据库连接配置


DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <environments default="mysql">
 <environment id="mysql">
 <transactionManager type="JDBC"/>
 <dataSource type="POOLED">
 <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
 <property name="username" value="root"/>
 <property name="password" value="Yjb123456"/>
 dataSource>
 environment>
 environments>
 <mappers>
 <mapper resource="com/itheima/mapper/CustomerMapper.xml"/>
 mappers>
configuration>

你可能感兴趣的:(遇到的问题,mysql,数据库,java)