第一个 Mybatis程序--测试中可能遇到的问题以及解决方案
Mybatis程序,测试中可能遇到的问题以及解决方案:
-
错误一:找不到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 的路径写的是否正确
-
错误二: java.lang.ExceptionInInitializerError
在mapper.xml文件中写了中文注释,导致报错
解决方案:将头文件中的
encoding="UTF-8"
改成UTF8重新运行后问题解决
-
错误三:
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&useSSL=true&useUnicode=true&characterEncoding=utf-8"/> -
错误四:
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),修改正确后即可。
本文来自博客园,作者:望穿先生,转载请注明原文链接:https://www.cnblogs.com/wangchuanxiansheng/p/16005114.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了