1. 项目架构如下
项目主要分为3层,controller层、service层、dao层
2.Controller层代码
controller层主要负责接收前端发来的请求,将其json格式的数据转换成对象,然后调用service层的服务。
package com.whut.controller;
import com.whut.bean.User;
import com.whut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
int id=userService.login(user.getUsername(),user.getPassword());
ModelAndView mv=new ModelAndView();
if(id!=-1){
mv.setViewName("main");
}else {
mv.setViewName("../failer");
}
return mv;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> users=userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("users",users);
mv.setViewName("