运行时发现报错信息是169行

然后,检查169行的代码(自然包括169行调用的代码啦)并没有发现明显的问题,然后我们可以注释掉169行的代码,然后重新运行,比如:

此时的运行报错信息如下:


    "C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=54192:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\plugins\junit\lib\junit5-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;E:\java\mybatis\target\test-classes;E:\java\mybatis\target\classes;C:\Users\dell\.m2\repository\org\mybatis\mybatis\3.5.1\mybatis-3.5.1.jar;C:\Users\dell\.m2\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar;C:\Users\dell\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\dell\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\dell\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.mybatis.MyBatisTestor,testInsert

    java.lang.ExceptionInInitializerError
    	at com.imooc.mybatis.MyBatisTestor.testInsert(MyBatisTestor.java:152)
    	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: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: org.apache.ibatis.exceptions.PersistenceException:
    ### Error building SqlSession.
    ### The error may exist in mappers/goods.xml
    ### The error occurred while processing mapper_resultMap[rmGoods]
    ### 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 'mappers/goods.xml'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for goods.insert. please check mappers/goods.xml and mappers/goods.xml
    	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52)
    	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36)
    	at com.imooc.mybatis.utils.MyBatisUtils.<clinit>(MyBatisUtils.java:22)
    	... 23 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 'mappers/goods.xml'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for goods.insert. please check mappers/goods.xml and mappers/goods.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:50)
    	... 25 more
    Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'mappers/goods.xml'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for goods.insert. please check mappers/goods.xml and mappers/goods.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)
    	... 27 more
    Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for goods.insert. please check mappers/goods.xml and mappers/goods.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)
    	... 30 more


    Process finished with exit code -1

通过上面的报错信息,可以帮助我们定位错误:

在goods.xml中果然发现了重复的insert;;;;

将其修改后:

再次运行就OK了:

即,这个报错信息背后的原因,说白了就是xml配置文件出错了,导致MybatisUtils类(这个类中包含初始化SqlSessionFactory这个对象;获得SqlSession对象的方法;关闭SqlSession对象的方法;的内容;;;即这个类的主要功能是初始化Mybatis)在动态运行初始化加载Mybatis的时候出错了。(PS:mybatis的配置文件出错了,自然Mybatis的初始化加载会出错了~~~)


(1)声明一下,可能有很多原因都可以导致【java.lang.NoClassDefFoundError: Could not initialize class】。本篇博客,可以帮助提升【如何定位错误的能力】。

(2) 然后,MyBatisUtils类是工具类,中间后很多静态变量和静态代码块:可能有些在加载类或者静态变量或者静态代码块执行的时候,也会报【java.lang.NoClassDefFoundError: Could not initialize class】,但是这涉及到了JVM的一些东西,目前并不能很好的理解,望在以后能更好的理解这儿的定西。

 
     package com.imooc.mybatis.utils;
 
     import org.apache.ibatis.io.Resources;
     import org.apache.ibatis.session.SqlSession;
     import org.apache.ibatis.session.SqlSessionFactory;
     import org.apache.ibatis.session.SqlSessionFactoryBuilder;
     import sun.misc.ExtensionInstallationException;
 
     import java.io.IOException;
     import java.io.Reader;
 
     /**
      * MyBatis工具类
      * (1)创建全局唯一的SqlSessionFactory对象;(2)获取SqlSession的方法;(3)关闭SqlSession的方法;
      */
     public class MyBatisUtils {
         //sqlSessionFactory对象设置成静态的,这个对象属于类的;
         private static SqlSessionFactory sqlSessionFactory = null;
         static{
             try {
                 Reader reader = Resources.getResourceAsReader("mybatis-
 config.xml");
                 sqlSessionFactory = new
 SqlSessionFactoryBuilder().build(reader);
             } catch (IOException e) {
                 e.printStackTrace();
                 // 如果出现了异常,除了上面打印异常,还需要将这个异常向上抛,让使用这个类的程序也知道这儿报错了;
                 //
 这儿主动抛了ExceptionInInitializerError,即在类的初始化过程中产生了错误;即调用者捕获了这个异常,就能够明白,
                 // mybatis在初始化的时候产生了错误,
                 throw new ExceptionInInitializerError(e);
             }
         }
 
         /**
          * 获得SqlSession对象的方法;;;;
          * 在其他地方调用这个方法获得SqlSession对象后,后续就可以利用SqlSession完成数据表的增删改查了;
          * 说明:工具类中的方法,一般使用static进行描述,这样以后通过类名就能直接调用了;
          * @return
          */
         public static SqlSession openSession(){
             return sqlSessionFactory.openSession();
         }
 
         /**
          * 关闭SqlSession的方法;
          * @param sqlSession
          */
         public static void closeSession(SqlSession sqlSession){
             if (sqlSession != null){
                 sqlSession.close();
             }
         }
     }
 

(20210619加):

其也报了【java.lang.NoClassDefFoundError: Could not initialize class】;修改后,就OK了,,,,,但是,后来又把上面的xml该错后,又不报这个异常了,,,,挺操蛋的,其背后的根本原因尚不明确。