qwertyanglu 2014-07-29 23:56
浏览 194
已采纳

各位大大,谁能帮我看下我这个spring3.0 和 mybatis整合出了什么问题呀,mybatis总是行不成功,完整项目我已经放在附件中了

附件是我的项目,可以直接在eclipse中部署运行,用的mysql数据库,只需建一个test数据库就可以,建表语句在test.rar附件中,执行的service url是:http://localhost:8080/springTest/lyang/getAll.do

每次一执行就报:
java.lang.NullPointerException
at com.lyang.springTest.daoImpl.IPeopleDao.getAllPeople(IPeopleDao.java:15)
at com.lyang.springTest.service.IPeopleService.getAll(IPeopleService.java:18)
at com.lyang.springTest.controller.IPeopleController.getAll(IPeopleController.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:237)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

iteye里面大神比较多,希望各位前辈能给我指点一下,弄了两晚上了,一直这个错误,不知道什么原因,如果不用注解改成xml手动注入bean的话就没有问题,一用注解配置文件里面改成自动扫描,mybatis就执行不成功了

  • 写回答

3条回答 默认 最新

  • iteye_13813 2014-07-30 10:26
    关注

    楼主,首先你web.xml有点问题,有个参数你配置了两遍,我改了。
    然后 你最大的问题是 ,数据库session没有注入,见
    spring-mybatis.xml


    你把sqlSessionFactory注入给了org.mybatis.spring.SqlSessionTemplate,
    而sqlSession却没有注入给你的BasicSqlSupport这个类,而BasicSqlSupport却需要给子类IPeopleDao提供sqlSession,故为空。 解决方案见下
    把上面的那个删除掉,添加


    然后把BasicSqlSupport 改为如下
    public class BasicSqlSupport {

    private SqlSessionFactory sqlSessionFactory;
    public SqlSession session;
    
    public SqlSession getSqlSession() {
        return session;
    }
    
    public void setSqlSession(SqlSession sqlSession) {
        this.session = sqlSession;
    }
    
    public SqlSessionFactory getMySqlSessionFactory() {
        return sqlSessionFactory;
    }
    
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        session = sqlSessionFactory.openSession();
    }
    

    }

    当然,后者我也有问题,不够简洁,所有 还可以改下,但这样至少能解决你的问题,我的结果是

    1 lyang 23
    2 cchuang 23
    0 user0 20
    1 user1 21
    2 user2 22
    3 user3 23
    4 user4 24
    5 user5 25
    6 user6 26
    7 user7 27
    8 user8 28
    9 user9 29
    不知道是不是你想要的!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值
  • ¥20 数据排序,可选择排序方向
  • ¥15 修改一下代码,考虑进程到达时间不同的情况
  • ¥15 帮我看看这是个啥题,带解题过程和结果,条件如下FCF = 290471.33 g1 = 15% r = 8% g2 = 4% n = 5
  • ¥15 edem模拟颗粒不显示或者生成失败
  • ¥15 Python代码编写