工具类无法初始化java.lang.NoClassDefFoundError:

来源:3-1 OneToMany对象关联查询

明月_0

2020-02-26 23:41:41

package com.i.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 java.io.IOException;
import java.io.Reader;

/**
* MybatisUtils工具类,创建全局唯一的SqlSessionFactory对象
*/
public class MybatisUtils {
   //利用static(静态)属于类不属于对象,且全局唯一
   private static SqlSessionFactory sqlSessionFactory = null;
   //利用静态块在初始化类时实例化sqlSessionFactory
   static {
       Reader reader = null;
       try {
           reader = Resources.getResourceAsReader("mybatis-config.xml");
           System.out.println("1231");
           sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
       } catch (IOException e) {
           e.printStackTrace();
           //初始化错误时,通过抛出异常ExceptionInInitializerError通知调用者
           throw new ExceptionInInitializerError(e);
       }
   }

   /**
    * openSession 创建一个新的SqlSession对象
    * @return SqlSession对象
    */
   public static SqlSession openSession(){
       //默认SqlSession会自动提交事务数据(commit)
       //设置false代表关闭自动提交,改为手动提交事务数据
       return  sqlSessionFactory.openSession(false);
   }

   /**
    * 释放一个有效的SqlSession对象
    * @param session 准备释放SqlSession对象
    */
   public static void closeSession(SqlSession session){
       if(session != null){
           session.close();
       }
   }
}


<resultMap id="rmGoodsDetail" type="com.i.mybatis.entity.GoodsDetail">
   <id column="gd_id" property="gdId"></id>
   <association property="goods" select="goods.selectById" column="goods_id"></association>
</resultMap>
<select id="selectManyToOne" resultMap="rmGoodsDetail">
   select * from t_goods_detail limit 0,1
</select>


/**
* 测试多对一对象关联映射
*/
@Test
public void testManyToOne() throws Exception {
   SqlSession session = null;
   try {
       session = MybatisUtils.openSession();
       List<GoodsDetail> list = session.selectList("goodsDetail.selectManyToOne");
       for(GoodsDetail gd:list) {
           System.out.println(gd.getGdPicUrl() + ":" + gd.getGoods().getTitle());
       }
   } catch (Exception e) {
       throw e;
   } finally {
       MybatisUtils.closeSession(session);
   }
}



它说我无法初始化类,以前能执行的测试方法都不能执行了,target目录下都编译起了,为什么会这样了,



java.lang.NoClassDefFoundError: Could not initialize class com.i.mybatis.utils.MybatisUtils


at com.i.mybatis.MyBatisTestor.testManyToOne(MyBatisTestor.java:268)

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)



Process finished with exit code -1


写回答

1回答

好帮手慕小班

2020-02-27

同学你好,测试贴出代码,因为同学的resultMap 不一致,老师测试与同学的很多参数不一致,导致resultMap没有代入正常测试,测试其他代码,是可以正常输出的,例如:

http://img1.sycdn.imooc.com/climg/5e57723f092be32913060656.jpg

如上所示,goods.xml编写的有点小问题,一对多的column属性关联的字段有问题,同学可以排查rmGoodsDetail中条件一一对应的字段,这里老师的代码如下,同学可以参考修改:

<resultMap id="rmGoods1" type="com.imooc.mybatis.entity.Goods">
    <!-- 映射goods对象的主键到goods_id字段 -->
    <id column="goods_id" property="goodsId"></id>
    <!--
        collection的含义是,在
        select * from t_goods limit 0,1 得到结果后,对所有Goods对象遍历得到goods_id字段值,
        并代入到goodsDetail命名空间的findByGoodsId的SQL中执行查询,
        将得到的"商品详情"集合赋值给goodsDetails List对象.
    -->
    <collection property="goodsDetails" select="goodsDetail.selectByGoodsId"
                column="goods_id"/>
</resultMap>

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

0

0 学习 · 8016 问题

查看课程