关于mybatis测试类测试时报NoClassDefFoundError的异常

问题描述:工程里有一个MyBatisUtil.java的工具类,封装用来对SqlSession进行openSqlSession和close的,

工具类中通过流获取mybatis-config.xml的配置文件

InputStream is= Resources.getResourceAsStream(“mybatis-config.xml”);

然后把相应的mapper.xml都写好了 select语句都没问题 进行测试时Junit里报java.lang.NoClassDefFoundError: Could not initialize class
cn.smbms.utils.MyBatisUtil

MyBatisUtil类代码如下



public
class MyBatisUtil {

      private static SqlSessionFactory factory;

       static{

             InputStream is=null;

            try {

                  is =
Resources.getResourceAsStream("mybatis-config.xml");

                  factory=new
SqlSessionFactoryBuilder().build(is);

            } catch (IOException e) {

                  // TODO Auto-generated catch
block

                  e.printStackTrace();

            }finally{

                  try {

                        is.close();

                  } catch (IOException e) {

                        // TODO Auto-generated
catch block

                        e.printStackTrace();

                  }

            }

       }

       

       public static SqlSession createSqlSession(){

             return factory.openSession(true);

       }

       

       public static void closeSqlSession(SqlSession
sqlSession){

             if(null!= sqlSession){

                   sqlSession.close();

             }

       }

}


经过多次检查MyBatisUtil,重新编绎等  问题还是没有得到解决

因为mapper.xml里写了多条select语句 ,再未添加最后一条select语句之前,测试是没有异常的,初步断定应该是最后一条语句有问题,然后把最后一条select语删除了,果然,测试其它的正常了,添上最后一条,测试其它的还是报之前的错误。最终确定到问题点了,最后写的select语句惹的祸,经过仔细检查,果然找到问题了

select
u.id,u.userName,u.gender from smbms_user u

and
u.userRole=#{userRole}

and
userName like CONCAT(’%’,#{userName},’%’)

其中resulType="user"属性给写错了  resultType  少写了一个t   导致MyBatisUtil加载时,其中的静态代码块在类加载时便会编绎执行,在编绎时读取mybatis-config.xml的配置文件,mybatis-config.xml中配置了mapper。Mapper中的select出错,最终导致MyBatisUtil.java的工具类加载失败

把mapper文件中错误改正,问题得到解决

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿松哥哥2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值