no_russion 2019-11-13 17:11 采纳率: 25%
浏览 701
已采纳

mybatis连接数据库失败是什么原因,百度了半天没找到有用的办法

下面的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 + '\'' + '}';
    }
}

  • 写回答

1条回答 默认 最新

  • zhshchilss 2019-11-13 17:24
    关注

    loacalhost--》localhost 是不是你单词写错了大兄弟

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus