ExceptionInInitializerError与PersistenceException异常的出现和解决
刚开始学习MyBatis框架,正在进行【MyBatis 使用传统 Dao 开发方式】,运行测试方法testSelect,查询数据库中所有学生信息,但是最后结果出现了错误:
java.lang.ExceptionInInitializerError
可以看到后面还有一个异常
Caused by: org.apache.ibatis.exceptions.PersistenceException:
Error building SqlSession.
The error may exist in com/yuming/dao/StudentDao.xml
运行截图:
完整异常信息:
java.lang.ExceptionInInitializerError
at com.yuming.dao.StudentDaoImpl.selectStudents(StudentDaoImpl.java:13)
at com.yuming.TestMyBatis.testSelect(TestMyBatis.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/yuming/dao/StudentDao.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/yuming/dao/StudentDao.xml'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.yuming.dao.StudentDao.. please check com/yuming/dao/StudentDao.xml and com/yuming/dao/StudentDao.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.yuming.util.MyBatisUtil.<clinit>(MyBatisUtil.java:21)
... 24 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/yuming/dao/StudentDao.xml'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.yuming.dao.StudentDao.. please check com/yuming/dao/StudentDao.xml and com/yuming/dao/StudentDao.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)
... 26 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/yuming/dao/StudentDao.xml'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.yuming.dao.StudentDao.. please check com/yuming/dao/StudentDao.xml and com/yuming/dao/StudentDao.xml
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:122)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:94)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:373)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
... 28 more
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.yuming.dao.StudentDao.. please check com/yuming/dao/StudentDao.xml and com/yuming/dao/StudentDao.xml
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:930)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:887)
at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:686)
at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:296)
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:110)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:137)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:130)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
... 31 more
然后结果多次debug运行测试,发现确实是我com/yuming/dao/StudentDao.xml 这个sql映射文件里面的问题,因为我当时想着一次把别的几个标签也写上了,只不过把id的值先空着在,sql语句也没写,到时候用的时候直接填:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yuming.dao.StudentDao">
<select id="selectStudents" resultType="com.yuming.entity.Student">
select id,name,email,age from student order by id
</select>
<insert id="">
</insert>
<delete id="">
</delete>
<update id="">
</update>
</mapper>
然后就是因为我这样做了,导致后面一直不能运行成功,当我把它们删掉之后,就OK了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yuming.dao.StudentDao">
<select id="selectStudents" resultType="com.yuming.entity.Student">
select id,name,email,age from student order by id
</select>
</mapper>
果然有的时候不能太偷懒啊。。
注:我在出现ExceptionInInitializerError异常的时候也百度了很久,发现出现原因有很多种,并没有看到我这种的,目前不知道我的解决方法对不对,先做个记录在这里