MyBatis数据库查询时配置错误问题

异常信息:

### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 510 milliseconds ago.  The last packet sent successfully to the server was 504 milliseconds ago.
### The error may exist in com/lidongyang/dao/UserMapper.xml
### The error may involve com.lidongyang.dao.UserDao.getUserList
### The error occurred while executing a query
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
 

这个情况问题,我找了很久,通过后面两句的意思应该是链接数据库查询的时候出了错。

这样就应该是配置文件有问题

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

<!--    每一个Mapper.xml都需要Mybatis核心配置文件中注册-->
    <mappers>
        <mapper resource="com/lidongyang/dao/UserMapper.xml"/>
    </mappers>
</configuration>

通过测试是安全访问方式的错误。这里将useSSL的值改为false就可以了。

不知道原理,有大佬知道原理留个言。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 在MyBatis中,可以使用自带的连接池或者第三方连接池来配置数据库连接。如果你想使用MyBatis自带的连接池,可以在MyBatis配置文件(通常是mybatis-config.xml)中设置数据源的type为POOLED。在这个配置文件中,你需要指定数据库驱动、URL、用户名和密码等连接参数。\[3\]这样配置后,MyBatis会使用自带的连接池来管理数据库连接。另外,如果你使用的是第三方连接池,可以根据具体的连接池的文档进行配置。在配置连接池,需要注意数据库连接的超间。例如,MySQL默认的连接超间是8小,如果连接超后程序还在使用该连接,就会导致错误。为了解决这个问题,可以使用连接池技术来管理连接,确保连接在超后能够被正确关闭并重新创建。\[2\] #### 引用[.reference_title] - *1* *2* [数据库连接失效导致程序异常怎么办—-mybatis数据库连接池配置” 数据库连接失效导致程序异常怎么办—-...](https://blog.csdn.net/qq_36370053/article/details/81534752)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [mybatis数据库连接池](https://blog.csdn.net/m0_62520968/article/details/124692646)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值