下面的Communications link failure如何解决,检查过IUserMapper的xml问句和IUserDao的findAll是按照视频敲的
Exception in thread "main" 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/HSY/Mapper/IUserMapper.xml
### The error may involve com.HSY.dao.IUserDao.findAll
### The error occurred while executing a query
### Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
这个是Mybatis配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis主配置文件 -->
<configuration>
<!--default和id相同 -->
<environments default="mysql">
<environment id="mysql">
<!--配置事务类型为JDBC -->
<transactionManager type="JDBC"></transactionManager>
<!--配置连接池,即要连接的数据库信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://loacalhost:3306/mybatistest"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--指定映射配置文件的位置 -->
<mappers>
<mapper resource="com/HSY/Mapper/IUserMapper.xml"/>
</mappers>
</configuration>
这个是程序入口
package com.HSY.test;
import com.HSY.dao.IUserDao;
import com.HSY.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest
{
public static void main(String[] args) throws IOException
{
//读取配置文件
InputStream inputStream= Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(inputStream);
//实用工厂生成SqlSession对象
SqlSession session=factory.openSession();
//使用SqlSession创建Dao接口的代理对象
IUserDao userDao = session.getMapper(IUserDao.class);
//使用代理对象执行方法
List<User> userList=userDao.findAll();
for(User user:userList)
{
System.out.println(user.toString());
}
//释放资源
session.close();
inputStream.close();
}
}
这是IUserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 必须是Dao接口的全限定类名 -->
<mapper namespace="com.HSY.dao.IUserDao">
<!-- SQL语句 -->
<!-- 下面的id指方法的名称id而不是数据库里的人员id-->
<select id="findAll" resultType="com.HSY.domain.User">
select * from user
</select>
</mapper>
这是IUserDo
import com.HSY.domain.User;
import java.util.List;
public interface IUserDao
{
List<User> findAll();
}
还有User类
package com.HSY.domain;
public class User
{
private Integer id;
private String username;
private String bitrthday;
private String sex;
private String address;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getBitrthday()
{
return bitrthday;
}
public void setBitrthday(String bitrthday)
{
this.bitrthday = bitrthday;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public String toString()
{
return "User{" + "id=" + id + ", username='" + username + '\'' + ", bitrthday='" + bitrthday + '\'' + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}';
}
}