这节课我们学了SSM框架增删改查。
新建文件:
User中的内容还是跟以前一样。
UserController中代码:
package com.zhongruan.controller;
import com.github.pagehelper.PageInfo;
import com.zhongruan.bean.User;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int size) {
List<User> users=userService.findAll(pageNum,size);
PageInfo<User> pageInfo=new PageInfo<>(users);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("allUser.jsp");
modelAndView.addObject("pageInfo",pageInfo);
return modelAndView;
}
@RequestMapping("/login.do")
public String login(User user){
Boolean flag=userService.login(user.getUsername(),user.getPassword());
if(flag){
return "redirect:/findAll.do";
}else{
return "failure.jsp";
}
}
@RequestMapping("/delete.do")
public String deleteById(int id){
userService.deleteById(id);
return "redirect:/findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
User user=userService.selectById(id);
ModelAndView mv=new ModelAndView();
mv.addObject("user",user);
mv.setViewName("updateUser.jsp");
return mv;
}
@RequestMapping("/addUser.do")
public String addById(User user) {
userService.addById(user);
return "redirect:/findAll.do";
}
@RequestMapping("/toupdate.do")
public ModelAndView toupdate(User user) {
int id = user.getId();
User users = userService.findById(id);
ModelAndView mv = new ModelAndView();
mv.setViewName("updateUser.jsp");
mv.addObject("users", users);
return mv;
}
@RequestMapping("update.do")
public String update(User user){
userService.update(user);
return "redirect:/findAll.do";
}
}
IUserDao中代码:
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import java.util.L