• Mybatis 异常:Cause: java.io.IOException: Could not find resource com.xxx.xxx.xml


    转载请注明出处:

    https://www.cnblogs.com/Xi-Chen00/p/12619072.html

    最近在复习mybatis知识的时候,遇到这个异常:

    Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
    ### Error building SqlSession.
    ### The error may exist in com/spring/repository/AccountRepository.xml
    ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/spring/repository/AccountRepository.xml
    	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
    	at com.spring.test.Test.main(Test.java:21)
    Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/spring/repository/AccountRepository.xml
    	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
    	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
    	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
    	... 2 more
    Caused by: java.io.IOException: Could not find resource com/spring/repository/AccountRepository.xml
    	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
    	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
    	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:371)
    	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
    	... 4 more
    
    Process finished with exit code 1
    
    

    检查运行类配置文件名,确定没错:

    检查mybatis的配置文件,注册的xml的路径确定没写错(路径用/,有.xml后缀):

    检查了.xml映射的namespace,确定也没写错(路径用. , 没有后缀):

    无论反复检查代码,也导入了mybatis依赖。最后想了一下,找不到文件是不是因为maven工程没有扫描到xml文件?(这点还是容易被忽略的,maven默认扫描resource目录)

    在pom的build中添加扫描java目录下xml文件的依赖就成功了:

        <resources>
          <resource>
            <directory>src/main/java</directory>
            <includes>
              <include>**/*.xml</include>
            </includes>
          </resource>
        </resources>
    
  • 相关阅读:
    存储:块存储/文件存储/对象存储
    系统调用system call以及strace/dtruss
    JBoss EAP应用服务器部署方法和JBoss 开发JMS消息服务小例子
    在IE中,JS方法名和input的name重名时,调用该方法无效
    html 使表格随着内容自动适应宽度
    Jboss 安全和优化
    java网页数据抓取实例
    Eclipse+Weblogic 12开发简单的Enterprise Application
    eclipse中整合ejb和web工程
    破解 jar 包之直接修改 .class 文件方式
  • 原文地址:https://www.cnblogs.com/Xi-Chen00/p/12619072.html
Copyright © 2020-2023  润新知