Cause: java.io.IOException: Could not find resource xxxx/xxx/xxx/xx.xml

这是没有加载到mybatis配置文件导致的异常,我在非常确定我在加载的映射文件中没有写错资源配置地址,百度找了很多解决方法都是说是用/ 和用 . 隔开路径的区别,但是一直以来,包的子目录之间我都是用 / 隔开,也无一出现这种情况。
我的资源位置如下
Cause: java.io.IOException: Could not find resource xxxx/xxx/xxx/xx.xml_第1张图片
配置文件中配置的路径


    <mappers>
        <mapper resource="com/test/mapper/UserMapper.xml">mapper>
    mappers>

报错如下
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/test/mapper/UserMapper.xml

在试了网络上的各种方法都不行之后,我才开始独立思考,是不是有可能不存在这个路径呢?于是我找到target目录,发现了不可思议的东西。
Cause: java.io.IOException: Could not find resource xxxx/xxx/xxx/xx.xml_第2张图片
为什么同样是com是父级目录,test子目录却没有放到com里面?

真相只有一个
那就是我的包名就叫 com.test.mapper
果然啊,创建目录的时候不能用 xx.xx.xx 这样创建多级目录的,只有在创建xml文件时前面用 / 会自动创建多级目录。
所以,创建真正的com->test->mapper这样一个目录,再把配置文件拷贝到里面就解决了。又或者说,将配置路径设置成如下方式(当然这是不规范的)


    <mappers>
        <mapper resource="com.test.mapper/UserMapper.xml">mapper>
    mappers>

你可能感兴趣的:(异常处理,mybatis)