报错:Communications link failure(弄了好久没解决)

报错:Communications link failure(弄了好久没解决)

package com.wf.mybatis.entity;

public class Goods {
    private Integer goodsId;
    private String title;
    private String subTitle;
    private Float originalCost;
    private Float currentPrice;
    private Float discount;
    private Integer isFreeDelivery;
    private Integer categoryId;

    public Goods() {
    }

    public Goods(Integer goodsId, String title, String subTitle, Float originalCost, Float currentPrice, Float discount, Integer isFreeDelivery, Integer categoryId) {
        this.goodsId = goodsId;
        this.title = title;
        this.subTitle = subTitle;
        this.originalCost = originalCost;
        this.currentPrice = currentPrice;
        this.discount = discount;
        this.isFreeDelivery = isFreeDelivery;
        this.categoryId = categoryId;
    }

    public Integer getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSubTitle() {
        return subTitle;
    }

    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }

    public Float getOriginalCost() {
        return originalCost;
    }

    public void setOriginalCost(Float originalCost) {
        this.originalCost = originalCost;
    }

    public Float getCurrentPrice() {
        return currentPrice;
    }

    public void setCurrentPrice(Float currentPrice) {
        this.currentPrice = currentPrice;
    }

    public Float getDiscount() {
        return discount;
    }

    public void setDiscount(Float discount) {
        this.discount = discount;
    }

    public Integer getIsFreeDelivery() {
        return isFreeDelivery;
    }

    public void setIsFreeDelivery(Integer isFreeDelivery) {
        this.isFreeDelivery = isFreeDelivery;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }
}
=============================================================================================
package com.wf.mybatis.utils;

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.Reader;

/**
 * Mybatis工具类,创建全局唯一的SqlSessionFactory对象
 */
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory=null;
    static {
        Reader reader=null;
        try {
            reader=Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory= new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }
    public static void closeSession(SqlSession sqlSession){
        if(sqlSession!=null){
            sqlSession.close();
        }
    }
}
==============================================================================================
<?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">
<configuration>
    <settings>
        <!--    驼峰命名转换:goods_id->goodsId    -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--  默认指向的数据库  -->
    <environments default="dev">
        <!-- 环境配置:不同的环境不同的id-->
        <environment id="dev">
            <transactionManager type="JDBC"></transactionManager>
            <!-- 采用连接池的方式管理数据库连接-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/babytun?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="wf980321"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mappers/goods.xml"/>
    </mappers>
</configuration>
============================================================================================
<?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">
<mapper namespace="goods">
    <select id="selectAll" resultType="com.wf.mybatis.entity.Goods">
        select * from t_goods
    </select>
</mapper>
==============================================================================================
package com.wf.mybatis;

import com.wf.mybatis.entity.Goods;
import com.wf.mybatis.utils.MybatisUtils;
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 org.junit.Test;


import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class MybatisTestor {
    @Test
    public void mybatisUtilsTest() throws Exception {
        SqlSession sqlSession= null;
        try {
            sqlSession=MybatisUtils.getSession();
            System.out.println(sqlSession);

        } catch (Exception e) {
            throw e;
        }finally {
            MybatisUtils.closeSession(sqlSession);
        }

    }
    @Test
    public void selectAllTest() throws Exception {
        SqlSession sqlSession= null;
        try {
            sqlSession=MybatisUtils.getSession();
            List<Goods> list = sqlSession.selectList("goods.selectAll");
            for(Goods g:list){
                System.out.println(g.getTitle());
            }
        } catch (Exception e) {
            throw e;
        }finally {
            MybatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void test01() throws IOException {
        Reader reader= Resources.getResourceAsReader("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(reader);
        SqlSession session=null;
        session=sqlSessionFactory.openSession();
        System.out.println(session);
        if(session!=null){
            session.close();
        }
    }
}
================================================================================================
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wf</groupId>
    <artifactId>mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
    </dependency>
</dependencies>

</project>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小脸 2020-09-04 10:16:47

同学你好,经测试同学代码是正确的,这里根据同学的报错信息:Communications link failure  -->通信异常,通信链接失败。

这里是你数据库没有链接成功导致的。

1、确保数据库正常启动

2、数据库的地址,用户名密码,端口号正确

3、你在url后面加上

&amp;autoReconnect=true&amp;failOverReadOnly=false

上述步骤排查都未解决,建议将错误信息完整贴出。

祝学习愉快~

  • 提问者 wufeng321 #1
    还是没解决,完整错误信息: org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 29,179 milliseconds ago. The last packet sent successfully to the server was 29,148 milliseconds ago. ### The error may exist in mappers/goods.xml ### The error may involve goods.selectAll ### The error occurred while executing a query ### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    2020-09-05 09:15:33
  • 提问者 wufeng321 #2
    终于解决了,可能是mysql驱动的版本问题,我换成8.0.19就可以了,之前的5.几的版本一直不行
    2020-09-05 10:40:45
  • 好帮手慕小脸 回复 提问者 wufeng321 #3
    能够独立解决问题在开发过程中也是非常重要的,为同学点赞!继续加油吧! 祝学习愉快~
    2020-09-05 10:57:14
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师