报错:
java.lang.ExceptionInInitializerError | |
at com.dkx.mybatis01.util.MybatisUtilsTest.test(MybatisUtilsTest.java:21) | |
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.base/java.lang.reflect.Method.invoke(Method.java:566) | |
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) | |
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) | |
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) | |
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) | |
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) | |
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) | |
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) | |
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) | |
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) | |
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) | |
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) | |
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) | |
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) | |
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) | |
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) | |
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) | |
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) | |
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) | |
Caused by: java.lang.RuntimeException: org.apache.ibatis.exceptions.PersistenceException: | |
### Error building SqlSession. | |
### The error may exist in com/dkx/mybatis01/dao/UserMapper.xml | |
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/dkx/mybatis01/dao/UserMapper.xml | |
at com.dkx.mybatis01.util.MybatisUtils.<clinit>(MybatisUtils.java:26) | |
... 23 more | |
Caused by: org.apache.ibatis.exceptions.PersistenceException: | |
### Error building SqlSession. | |
### The error may exist in com/dkx/mybatis01/dao/UserMapper.xml | |
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/dkx/mybatis01/dao/UserMapper.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.dkx.mybatis01.util.MybatisUtils.<clinit>(MybatisUtils.java:24) | |
... 23 more | |
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/dkx/mybatis01/dao/UserMapper.xml | |
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:122) | |
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99) | |
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78) | |
... 25 more | |
Caused by: java.io.IOException: Could not find resource com/dkx/mybatis01/dao/UserMapper.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:375) | |
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120) | |
... 27 more |
解决方案:
maven 由于它的约定大于配置,我们之后可能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案
<!-- 在 build 中配置 resources, 来防止我们资源导出失败的问题 --> | |
<build> | |
<resources> | |
<resource> | |
<directory>src/main/resources</directory> | |
<includes> | |
<include>**/*.properties</include> | |
<include>**/*.xml</include> | |
</includes> | |
<filtering>true</filtering> | |
</resource> | |
<resource> | |
<directory>src/main/java</directory> | |
<includes> | |
<include>**/*.properties</include> | |
<include>**/*.xml</include> | |
</includes> | |
<filtering>true</filtering> | |
</resource> | |
</resources> | |
</build> |
# MapperRegistry 是什么
核心配置文件中注册 mappers
# 可能遇到的问题
配置文件没有注册
绑定接口错误
方法名不对
返回类型不对
Maven 导出资源问题