mybatis常遇问题

本文记录了在MySQL8.0环境下学习MyBatis时遇到的常见错误及其解决方案,包括调整数据库URL避免SSL错误、解决PublicKeyRetrieval错误以及处理Mapper注册问题和资源路径配置。通过设置useSSL=false、添加allowPublicKeyRetrieval=true以及正确配置mapper资源和pom.xml,可以解决这些问题。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

mybatis学习笔记


前言

以下实验环境均基于MYSQL 8.0,是我在学习mybatis时构建项目测试所遇到的问题,可能会有错误的理解,欢迎指出。

一、错误:org.apache.ibatis.exceptions.PersistenceException

原配置文件中链接数据库的URL如下:

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

重点在于useSSL=true,在MYSQL的高级版本中需要指明是否进行SSL连接。但是如果直接设置为true,会造成缺少证书的异常,所以建议在未经服务器身份验证的情况下不要建立SSL连接,即设置useSSL=false;修改结果如下:

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

二、错误:Public Key Retrieval is not allowed

原因:RSA公钥不可用
如果用户使用了 sha256_password 认证,密码在传输过程中必须使用 TLS 协议保护,但是如果 RSA 公钥不可用,可以在连接中通过 AllowPublicKeyRetrieval=True参数以允许客户端从服务器获取公钥;但是需要注意的是 AllowPublicKeyRetrieval=True可能会导致中间人攻击,从而获取到明文密码,所以默认是关闭的,必须显式开启。
解决方法:在上面的url中添加allowPublicKeyRetrieval=true

三、错误:org.apache.ibatis.binding.BindingException: Type interface xx.xx.xx is not known to the MapperRegistry.

原因:mybatis-config配置文件中,没有mapper resource
解决方法:插入mapper resource即可,如下:

   <mappers>
       <mapper resource="xx/xxx/xxx/UserMapper.xml"/>
   </mappers>

四、错误:org.apache.ibatis.exceptions.PersistenceException

原因:没有找到UserMapper.xml, maven项目中:一般情况下xml文件应该放在resources下,而不是java下,所以当配置文件放在java下时,可能会导致配置文件无法导出/生效。
如下图:UserMapper.xml没有放置在resources中,而是放在java文件夹中:
在这里插入图片描述

解决方法:在pom.xml文件中添加build配置resources

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

参考

mysql 建立SSL连接问题,设置useSSL=false显式禁用SSL
MySQL 8.0 Public Key Retrieval is not allowed 错误的解决方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值