Could not initialize class com.imooc.mybatis.utils.MyBatisUtils

Could not initialize class com.imooc.mybatis.utils.MyBatisUtils

# 具体遇到的问题
ava.lang.NoClassDefFoundError: Could not initialize class com.imooc.mybatis.utils.MyBatisUtils

# 报错信息的截图
http://img1.sycdn.imooc.com//climg/5febebb90967f33f19201080.jpg# 相关课程内容截图

# 尝试过的解决思路和结果
MyBatisUtils 静态库出错
# 粘贴全部相关代码,切记添加代码注释

package com.student.mybatis;

import com.student.mybatis.entity.Student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class MyBatisTestor {
@Test
   public void testMyBatisSelectAll(){
SqlSession sqlSession =null;
       try {
sqlSession = com.imooc.mybatis.utils.MyBatisUtils.openSession();
           List<Student> list = sqlSession.selectList("student.selectAll");
           for (Student student:list){
System.out.println(student);
           }
} catch (Exception e) {
e.printStackTrace();
       } finally {
com.imooc.mybatis.utils.MyBatisUtils.closeSqlSession(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;
//创建全局唯一的SqlSessionFactory对象
public class MyBatisUtils {
//利用静态属于类不属于对象,且全局唯一
   private  static SqlSessionFactory sqlSessionFactory = null;
   //利用静态块在初始化类时实例化SqlSessionFactory
   static {
//初始化静态对象
       try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
           sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
       } catch (IOException e) {
e.printStackTrace();
           System.out.println("enter");
           throw new ExceptionInInitializerError(e);  //告诉调用者是初始化时出错
       }
}
public static SqlSession openSession(){
return sqlSessionFactory.openSession();
   }
public static void closeSqlSession(SqlSession sqlSession){
if(sqlSession != null){
sqlSession.close();
       }
}
}
package com.student.mybatis.entity;

public class Student {
private  Integer id;
   private Integer regNo;
   private String sex;
   private Integer age;
   private String grage;
   private String major;

   public Integer getId() {
return id;
   }

public void setId(Integer id) {
this.id = id;
   }

public Integer getRegNo() {
return regNo;
   }

public void setRegNo(Integer regNo) {
this.regNo = regNo;
   }

public String getSex() {
return sex;
   }

public void setSex(String sex) {
this.sex = sex;
   }

public Integer getAge() {
return age;
   }

public void setAge(Integer age) {
this.age = age;
   }

public String getGrage() {
return grage;
   }

public void setGrage(String grage) {
this.grage = grage;
   }

public String getMajor() {
return major;
   }

public void setMajor(String major) {
this.major = major;
   }

@Override
   public String toString() {
return "Student{" +
"id=" + id +
", regNo=" + regNo +
", sex='" + sex + '\'' +
", age=" + age +
", grage='" + grage + '\'' +
", major='" + major + '\'' +
'}';
   }
}
<?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="mapUnderscoreToCameCase" value="true"/>
   </settings>
   <!--默认指向数据库-->
   <environments default="student">
       <environment id="dev">
           <!--采用JDBC方式对数据库事务进行commmit/rollback -->
           <transactionManager type="JDBC"></transactionManager>
           <!--采用连接池方式管理数据库连接 -->
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&amp;characterEncoding=UTF-8"/>
               <property name="username" value="root"/>
               <property name="password" value="12358"/>
           </dataSource>
       </environment>
       <environment id="student">
           <!--采用JDBC方式对数据库事务进行commmit/rollback -->
           <transactionManager type="JDBC"></transactionManager>
           <!--采用连接池方式管理数据库连接 -->
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://localhost:3306/studb?useUnicode=true&amp;characterEncoding=UTF-8"/>
               <property name="username" value="root"/>
               <property name="password" value="12358"/>
           </dataSource>
       </environment>
   </environments>
   <mappers>
       <mapper resource="mappers/student.xml"/>
   </mappers>
</configuration>
<?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="student">
   <!--select id查询别名 resultType返回结果-->
   <select id="selectAll" resultType="com.student.mybatis.entity.Student">
         select * from student
</select>
</mapper>
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;
//创建全局唯一的SqlSessionFactory对象
public class MyBatisUtils {
//利用静态属于类不属于对象,且全局唯一
   private  static SqlSessionFactory sqlSessionFactory = null;
   //利用静态块在初始化类时实例化SqlSessionFactory
   static {
//初始化静态对象
       try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
           sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
       } catch (IOException e) {
e.printStackTrace();
           System.out.println("enter");
           throw new ExceptionInInitializerError(e);  //告诉调用者是初始化时出错
       }
}
public static SqlSession openSession(){
return sqlSessionFactory.openSession();
   }
public static void closeSqlSession(SqlSession sqlSession){
if(sqlSession != null){
sqlSession.close();
       }
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <repositories>
       <repository>
           <id>aliyun</id>
           <name>aliyun</name>
           <url>https://maven.aliyun.com/repository/central</url>
       </repository>
   </repositories>
   <groupId>StudentMybatis</groupId>
   <artifactId>com.student.mybatis</artifactId>
   <version>1.0-SNAPSHOT</version>
   <dependencies>
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>3.5.1</version>
       </dependency>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.47</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
       </dependency>
   </dependencies>

</project>


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

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

1回答
好帮手慕小脸 2020-12-30 11:52:11

同学你好,在mybatis-config.xml中,如下单词书写错误,缺少一个字母l,修改代码如下所示:

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

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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