springmvc整合mybatis连接mysql是访问页面的错误信息

[b]自己在搭一个mvc框架,在加入mybatis之后访问页面时错误信息如下:[/b]
type Exception report

message Request processing failed; nested exception is java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
zhha.controller.WelcomeController.Welcome(WelcomeController.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

请问这是什么问题引起的,希望得到一些解决方法的指点。附件为整个框架包。
UserService.java
[code="java"]
package zhha.service;

import java.util.List;

import zhha.dao.UserDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zhha.model.User;

@Service
public class UserService
{
@Autowired
private UserDao userDao;

public List<User> showUser()
{
    userDao.select();
    List<User> userList  = userDao.select();  
    return userList;
}

}

[/code]

User.java
[code="java"]

package zhha.model;

import java.io.Serializable;

public class User implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;


private String id;
private String name;
private String password;
public String getId() {
    return id;
}
public String getName() {
    return name;
}
public String getPassword() {
    return password;
}
public void setId(String id) {
    this.id = id;
}
public void setName(String name) {
    this.name = name;
}
public void setPassword(String password) {
    this.password = password;
}

}
[/code]

UserDao.java
[code="java"]
package zhha.dao;

import java.util.*;
import zhha.model.User;

public interface UserDao
{
public List select();
}
[/code]

UserController.java
[code="java"]
package zhha.controller;

import java.util.List;

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

import zhha.model.User;
import zhha.service.UserService;;

@Controller
@RequestMapping("/")
public class WelcomeController
{
private UserService userService;
@RequestMapping({"/welcome","/"})

public String Welcome()
{
List users = userService.showUser();
System.out.println(users.size());
return "/welcome";
}

@RequestMapping("/hello")
public String Hello()
{
      System.out.println("hello--------------------");
      return "/hello";
}

}
[/code]

1个回答

这里private UserService userService; 没有自动注入啊

nimasike
温故而知新666 和mybatis没有关系, 因为你用的Spring 没有注入的话 userService为null 就报错了。
5 年多之前 回复
lifegene
lifegene 我是刚开始学,为什么没有自动注入就会产生这个错误
5 年多之前 回复
lifegene
lifegene 高人,太感谢了,一眼就看出问题,加上自动注入后,数据访问信息可以返回了。
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问