第一个 Mybatis程序--测试中可能遇到的问题以及解决方案

Mybatis程序,测试中可能遇到的问题以及解决方案:

  1. 错误一:找不到mybatis配置文件java.io.IOException: Could not find resource org/mybatis/example/mybatis-config.xml

    解决方案

    在pom.xml中增加build

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

    如果该方法仍然无法解决找不到配置文件的话,(1)再注意自己的mapper文件,在mybatis-config.xml配置文件中有没有注册;(2)查看自己的mybatis的工具类(MybatisUtils.java)中resources 的路径写的是否正确

  2. 错误二: java.lang.ExceptionInInitializerError

    在mapper.xml文件中写了中文注释,导致报错

    解决方案:将头文件中的encoding="UTF-8" 改成UTF8

    重新运行后问题解决

  3. 错误三:

    org.apache.ibatis.exceptions.PersistenceException:

    Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure数据库连接问题

    解决方案

    将mybatis-config.xml核心配置文件中useSSL=true 改成false后问题解决

    <property name="url" value="jdbc:mysql://localhost:3305/mybatis?serverTimezone=GMT&amp;useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
    
  4. 错误四:

    org.apache.ibatis.binding.BindingException: Type interface com.lyl.dao.UserDao is not known to the MapperRegistry.

    UserMapper.xml文件中,namespace命名空间绑定问题

    <mapper namespace="com.lyl.dao.UserDao">
    

    解决方案

    查看自己的Dao/Mapper接口包的路径是否正确(路径要从java文件夹下开始写,com.xx.xx),修改正确后即可。

posted @ 2022-03-14 17:56  望穿先生  阅读(125)  评论(0编辑  收藏  举报