utils运行错误

utils运行错误

http://img1.sycdn.imooc.com//climg/5dcf5cc0092c205511390736.jpgpackage com.imooc.mybatis;

import com.imooc.mybatis.entry.Goods;
import com.imooc.mybatis.utils.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 org.junit.Test;

import java.io.Reader;
import java.sql.Connection;
import java.util.List;

public class MyBatisTest {
   @Test
   public void testSqlSessionFactory() throws Exception {
       Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
       System.out.println("SessionFactor加载成功");
       SqlSession sqlSession = null;
       try {
           sqlSession = sqlSessionFactory.openSession();
           Connection connection = sqlSession.getConnection();
           System.out.println(connection);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (sqlSession != null) {
               sqlSession.close();
           }
       }
   }
    @Test
       public void testSelectAll() throws Exception {
       SqlSession session = null;
           try {
               session= Utils.openSession();
               List<Goods> list = session.selectList("goods.selectAll");
               for (Goods g : list) {
                   System.out.println(g.getTitle());
               }

           } catch (Exception e) {
           throw e;
           } finally {
              Utils.closeSession(session);
           }


       }
   }

错误显示

"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:G:\idea\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar=62286:G:\idea\IntelliJ IDEA 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath "G:\idea\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar;G:\idea\IntelliJ IDEA 2019.2.4\plugins\junit\lib\junit-rt.jar;G:\idea\IntelliJ IDEA 2019.2.4\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;G:\idea_\target\test-classes;G:\idea_\target\classes;G:\Maven_store\org\mybatis\mybatis\3.5.1\mybatis-3.5.1.jar;G:\Maven_store\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar;G:\Maven_store\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;G:\Maven_store\junit\junit\4.12\junit-4.12.jar;G:\Maven_store\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.mybatis.MyBatisTest,testSelectAll





java.lang.NoClassDefFoundError: Could not initialize class com.imooc.mybatis.utils.Utils


at com.imooc.mybatis.MyBatisTest.testSelectAll(MyBatisTest.java:47)

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.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)



正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿满 2019-11-16 15:21:06

同学你好,测试同学的代码并没有什么问题,报错提示是运行是找不到Utils类,如:

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

这个可能是没有导入包,或者配置文件有问题造成的。

建议同学检查一下依赖是否引入,配置文件是否有问题。

祝:学习愉快~

  • 提问者 德里克rose #1
    老师 没有添加xml文件没有添加settings是可以打印出来的,在xml添加settings之后就报错了
    2019-11-17 10:08:12
  • 好帮手慕柯南 回复 提问者 德里克rose #2
    可能是同学那块settings那块有拼写错误,同学可以直接下载老师的源码,使用老师的配置文件替换。测试一下。祝学习愉快~
    2019-11-17 15:48:10
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师