springboot frame eclipse of environmental + mybatis null pointer being given access MySQL

In a recent write springboot project encountered a problem at the beginning, after I follow the tutorial to build a good framework for the use of mybatis access the MySQL database, but regardless of the implementation of the enhanced query the database or methods reported null pointer exception,

Error shots are as follows:

 

 

 2020-01-07 22:22:53.793 ERROR 2304 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

After performing a variety of solutions or not, has reported a null pointer, then looked at the code controller class, I started to be written so

@Controller
@RequestMapping("/")
public class TestController {

    
        @RequestMapping("/login")
        public String login() {        
            List<Duorou_goods> users = new ArrayList<Duorou_goods>();
            UserService userService = new UserService();
            users = userService.selectUser();

            return "html/login";
        }
        
}

I UserService statement on the method, the reason lies here, the object should be declared UserService outermost layer controller class, as properties of the declaration, such as:

@Controller
@RequestMapping("/")
public class TestController {

    @Autowired
    UserService userService;
        @RequestMapping("/login")
        public String login() {
            System.out.println("ok1");
            List<Duorou_goods> users = new ArrayList<Duorou_goods>();        
            users = userService.selectUser();            
            return "html/login";
        }
        
}

And to add @Autowried before UserService Object Notation job.

This problem has troubled me for a long time, after resolving not too sure what the principle is, also need to continue to learn.

Guess you like

Origin www.cnblogs.com/zhangliqiangvictory/p/12164084.html