SqlSessionFactoryBuilder ClassNotFoundException 1C

用@Test类测试没问题,但起项目就会报错,各位大神帮看一下什么原因

报错信息:

 严重: Servlet.service() for servlet [SpringServlet] in context with path [/gdesign] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSessionFactoryBuilder] with root cause
java.lang.ClassNotFoundException: org.apache.ibatis.session.SqlSessionFactoryBuilder
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at com.gdesign.tools.UserTools.getUserFromPk(UserTools.java:63)
    at com.gdesign.web.login.LoginController.Login(LoginController.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

java代码

    public User getUserFromPk(int pk) {
        String resource = "com/gdesign/mybatis/conf.xml";
        //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
        InputStream is = UserTools.class.getClassLoader().getResourceAsStream(resource);
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //创建能执行映射文件中sql的sqlSession
        SqlSession session = sessionFactory.openSession();
        String statement = "com.gdesign.mybatis.mapping.userMapper.getUser";//映射sql的标识字符串
        //执行查询返回一个唯一user对象的sql
        User user = session.selectOne(statement, pk);
        return user;
    }

配置文件

```<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.3//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

 <environments default="development">
     <environment id="development">
         <transactionManager type="JDBC" />
         <!-- 配置数据库连接信息 -->
         <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
         </dataSource>
     </environment>
 </environments>    
 <mappers>
     <!-- 注册userMapper.xml文件, -->
     <mapper resource="com/gdesign/mybatis/mapping/UserMapper.xml"/>
     <!-- 注册BuyOrderMapper.xml文件, -->
     <mapper resource="com/gdesign/mybatis/mapping/BuyOrderMapper.xml"/> 
 </mappers>

<?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">


resultType="com.gdesign.entity.User">
select * from user where pk_user = #{pk_user} and dr = 0

resultType="String">
select name from User where pk_user = #{pk_user} and dr = 0


select * from User where dr = 0






8个回答

在你的后台代码里,没有添加注解@Controller。。。。。。

LinS_Yang
LawsonYang 这段java代码不是Controller层的,代码可以进来,是new SqlSessionFactoryBuilder的时候报错
接近 3 年之前 回复

at com.gdesign.tools.UserTools.getUserFromPk(UserTools.java:63)
看看一看这里面涉及的都在配置文件里面配了没

LinS_Yang
LawsonYang 应该只有一个吧,看起来也是只有一个,要不然@test怎么好用?
接近 3 年之前 回复
qq_22260641
御前提笔小书童 回复LawsonYang: 那你看看SqlSessionFactory的jar有没有或者重复以及版本与其他的
接近 3 年之前 回复
LinS_Yang
LawsonYang 应该都配了,@test测试时候时好用的,但在项目中就不行了
接近 3 年之前 回复

ibatis-core的jar包有没有?

LinS_Yang
LawsonYang 没有,但有mybatis-3.3.0 jar包,两个都需要有吗
接近 3 年之前 回复

报错信息第八行,是你写的代码吗?

LinS_Yang
LawsonYang 是,那个就是Controller
接近 3 年之前 回复

对呀。那里面的25行报错。你写的什么内容报错呢

LinS_Yang
LawsonYang 只是调用这个函数,看我下面的评论
接近 3 年之前 回复

这就是那个类

 package com.gdesign.web.login;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdesign.entity.User;
import com.gdesign.tools.UserTools;

@Controller
@RequestMapping(value = "/login")
public class LoginController {
        @RequestMapping(value = "loginMethod", method = RequestMethod.POST)
        @ResponseBody
        public boolean Login(HttpServletRequest request){
            String userId = request.getParameter("userId");
            String password = request.getParameter("password");
            UserTools ut = new UserTools();
            try {
                User user = ut.getUserFromPk(Integer.valueOf(userId));
                if(user!=null&&user.getPassword().equals(password)){
                    //登录成功
                    //设置session
                    HttpSession session = request.getSession();
                    session.setAttribute("ID", user.getpk_user()+"");
                    session.setAttribute("name", user.getName());
                    return true;
                }
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
    }

你dbug进去看看进入方法之后哪一行报错。java.lang.ClassNotFoundException。。。。是不是你的 "com/gdesign/mybatis/conf.xml";文件没有找到。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问