java.lang.NoClassDefFoundError: Could not initialize class com解决方案

编写的时候遇到这样一个bug, java.lang.NoClassDefFoundError: Could not initialize class com

纠结了两天多,但是,没有找到答案,这个问题的多方提示就是

(1)缺JAR包 (2)JAR包冲突 (3)TOMCAT或Eclipse全路径中有空格

这些答案都没有解决我的问题,这个错误提示是说我的那个包没法启动,很多显示都是是什么静态定义必须trycatch,但是我的这个自己简历的公共类,这些都写了.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class MybatisUtil {
    private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
    private static SqlSessionFactory sqlSessionFactory;
 
    /**
     * 加载位于src/mybatis.xml配置文件
     */
    static {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 禁止外界通过new方法创建
     */
    private MybatisUtil() {
    }
 
    /**
     * 获取SqlSession
     */
    public static SqlSession getSqlSession() {
        //从当前线程中获取SqlSession对象
        SqlSession sqlSession = threadLocal.get();
        //如果SqlSession对象为空
        if (sqlSession == null) {
            //在SqlSessionFactory非空的情况下,获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession();
            //将SqlSession对象与当前线程绑定在一起
            threadLocal.set(sqlSession);
        }
        //返回SqlSession对象
        return sqlSession;
    }
 
    /**
     * 关闭SqlSession与当前线程分开
     */
    public static void closeSqlSession() {
        //从当前线程中获取SqlSession对象
        SqlSession sqlSession = threadLocal.get();
        //如果SqlSession对象非空
        if (sqlSession != null) {
            //关闭SqlSession对象
            sqlSession.close();
            //分开当前线程与SqlSession对象的关系,目的是让GC尽早回收
            threadLocal.remove();
        }
    }
 
}

然后,我进行了另一个测试,连接测试

1
2
3
4
5
6
7
8
public class TestConnection {
 
    @Test
    public void testConnection() throws Exception {
        Connection connection = MybatisUtil.getSqlSession().getConnection();
        System.out.println(connection!=null?"连接成功":"连接失败");
    }
}

发现是配置文件的错误,修改配置文件,连接就成功了,其他的就正确了

复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5 
 6     <properties resource="jdbc.properties"/>
 7 
 8     <!-- 别名标签 -->
 9     <typeAliases>
10         <typeAlias type="com.liuya.demo.mybatis.dysnamic.pojo.NewsLabel"
11                    alias="NewsLabel"/>
12     </typeAliases>
13 
14     <!-- 配置运行的数据库环境 -->
15     <environments default="mysqlenvironment">
16         <environment id="mysqlenvironment">
17             <!-- 連接池在本地连接中使用,在SSM中不用,用C3P0和DBCP -->
18             <transactionManager type="JDBC"/>
19             <dataSource type="POOLED">
20                 <property name="driver" value="${driver}"/>
21                 <property name="url" value="${url}"/>
22                 <property name="username" value="${username}"/>
23                 <property name="password" value="${password}"/>
24             </dataSource>
25         </environment>
26     </environments>
27 
28     <!-- 连接映射文件 -->
29     <mappers>
30         <!-- 最终使用的都是package -->
31         <mapper resource="com\liuya\demo\mybatis\dysnamic\mapper\NewsLabelMapper.xml"/>
32     </mappers>
33 </configuration>
View Code
复制代码

所以要好好的理解Mybatis的框架和配置标签的使用,这是好使版本的配置

 

posted @   北极的大企鹅  阅读(74887)  评论(5编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
阅读 - 79万
点击右上角即可分享
微信分享提示