zoukankan      html  css  js  c++  java
  • 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,但是我的这个自己简历的公共类,这些都写了.

    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();
            }
        }
    
    }
    

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

    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="comliuyademomybatisdysnamicmapperNewsLabelMapper.xml"/>
    32     </mappers>
    33 </configuration>
    View Code

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

  • 相关阅读:
    HDU2059(龟兔赛跑)
    pat 1012 The Best Rank
    pat 1010 Radix
    pat 1007 Maximum Subsequence Sum
    pat 1005 Sign In and Sign Out
    pat 1005 Spell It Right
    pat 1004 Counting Leaves
    1003 Emergency
    第7章 输入/输出系统
    第六章 总线
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/6811937.html
Copyright © 2011-2022 走看看