转载请注明出处:
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>