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


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

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

    解决方案

    在pom.xml中增加build

    
        
            
                src/main/resources
                
                    **/*.properties
                    **/*.xml
                
                false
            
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                false
            
        
    
    

    如果该方法仍然无法解决找不到配置文件的话,(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后问题解决

    
    
  4. 错误四:

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

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

    
    

    解决方案

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