Could not initialize class com.imooc.mybatis.utils.MyBatisUtils
# 具体遇到的问题
ava.lang.NoClassDefFoundError: Could not initialize class com.imooc.mybatis.utils.MyBatisUtils
# 报错信息的截图
# 相关课程内容截图
# 尝试过的解决思路和结果
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&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&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>
27
收起
正在回答 回答被采纳积分+1
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星