老师帮忙看下

来源:5-2 自由编程

慕沐0220913

2020-02-28 15:35:07

@Test
public void testInsert(){
   SqlSession sqlSession=null;
   try {
       sqlSession=MyBatisUtils.openSession();
       Goods goods=new Goods();
       goods.setRegNo(20171208);
       goods.setName("言豫津");
       goods.setSex("男");
       goods.setAge(26);
       goods.setGrade("2013");
       goods.setMajor("哲学系");
       //insert()方法返回值代表本次成功插入的记录次数
       int num =sqlSession.insert("goods.insert",goods);
       if(num>0){
           List<Goods> list=sqlSession.selectList("goods.selectAll");
           for (Goods g:list) {
               System.out.println(g);
           }
       }else{
           System.out.println("添加失败");
       }
       sqlSession.commit();
   }catch (Exception e){
       if(sqlSession!=null){
           sqlSession.rollback();
       }
       throw e;
   }finally {
       MyBatisUtils.closeSession(sqlSession);
   }
}

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

public class MyBatisUtils {
   //为保证唯一,利用static(静态)属于类,不属于对象,就可以保证唯一
   private static SqlSessionFactory sqlSessionFactory=null;
   //静态代码块可以初始化静态对象,
   static {
       Reader reader = null;
       try {
           reader = Resources.getResourceAsReader("mybatis-config.xml");
           sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);


       } catch (IOException e) {
           e.printStackTrace();
           //初始化错误时通过抛出异常ExceptionInInitializerError通知调用者
           throw new ExceptionInInitializerError(e);
       }
   }
       /**
        * openSession 创建一个新的 SqlSession对象
        */
       public static SqlSession openSession(){
           //默认sqlSession 对自动提交事务数据(commit)
           //设置false代表关闭自动提交,改为手动提交数据
           return sqlSessionFactory.openSession(false);
       }

   /**
    * 关闭连接
    * @param session
    */
   public static void closeSession(SqlSession session){
           if (session!=null){
               session.close();
           }
       }


}


<?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="goods">
   <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods">
       select * from student order by id desc limit 5
   </select>

   <insert id="insert" parameterType="com.imooc.mybatis.entity.Goods">
       INSERT INTO student(reg_no,name,sex,age,grade,major)
       VALUES (#{regNo},#{name},#{sex},#{age},#{grade},#{major})
       <selectKey resultType="Integer" keyProperty="Id" order="AFTER">
           select last_insert_id()
       </selectKey>
   </insert>
</mapper>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <!--goods_id-->goodsId即开启驼峰命名转换-->
   <settings>
       <setting name="mapUnderscoreToCamelCase" value="true"/>
   </settings>
   <environments default="dev">
       <environment id="dev">
           <transactionManager type="JDBC"></transactionManager>
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://localhost:3306/message?userSSL=false&amp;serverTimezone=UTC&amp;useUnicode=true&amp;CharacterEncoding=utf-8"/>
               <property name="username" value="root"/>
               <property name="password" value="18351337068cC"/>
           </dataSource>

       </environment>
   </environments>
   <mappers>
       <mapper resource="mappers/goods.xml"></mapper>
   </mappers>
</configuration>

报错

"C:\Program Files\Java\jdk-12.0.2\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54825:D:\JetBrains\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\JetBrains\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\JetBrains\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;D:\idea\mybatis\target\test-classes;D:\idea\mybatis\target\classes;D:\maven\repo\org\mybatis\mybatis\3.5.3\mybatis-3.5.3.jar;D:\maven\repo\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar;D:\maven\repo\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;D:\maven\repo\junit\junit\4.12\junit-4.12.jar;D:\maven\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.mybatis.MyBatisTestor,testInsert

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

 at com.imooc.mybatis.MyBatisTestor.testInsert(MyBatisTestor.java:103)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.base/java.lang.reflect.Method.invoke(Method.java:567)
 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


写回答

2回答

好帮手慕阿莹

2020-02-28

同学你好

1、首先这个控制台的报错只能说明是xml写的有错误了

2、而这里正好报错了,同学的注释多写了一个-->  要把中间的那个去掉

http://img.mukewang.com/climg/5e58ef1809ac4d1405490157.jpg

如果我的回答解决了你的问题,请采纳,祝学习愉快.

0

慕沐0220913

提问者

2020-02-28

http://img.mukewang.com/climg/5e58c83c096ab2e715140965.jpg

这还报错,之前查询还可以的,

0

0 学习 · 8016 问题

查看课程