SSM框架增删改查

首先先创建包的结构
在这里插入图片描述
在这里插入图片描述

UserController:

22:30:25
?? 2019/10/27 星期日 22:30:25

@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 users=userService.findAll(pageNum,size);
PageInfo 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:
public interface IUserDao {
List findAll();
User selectByUserName(String name);
void deleteById(int id);
User selectById(int id);
void update(User user);
void addById(User user);
}

UserService:

@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 users=userService.findAll(pageNum,size);
PageInfo 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”;

}
}

IUserService:

public interface IUserService {
List findAll(int pageNum,int size);
Boolean login(String username,String password);
void deleteById(int id);
void addById(User user);

void updateById(User user);
User findById(int id);

User selectById(int id);
void update(User user);
}

UserMapper.xml代码:

<?xml version="1.0" encoding="UTF-8"?> select * from tb_user select * from tb_user where username=#{username} select * from tb_user where id=#{id} update tb_user set username=#{username},password=#{password} where id=#{id} delete from tb_user where id=#{id} insert into tb_user(username,password) values (#{username},#{password})

addUser中加入:

用户姓名:


用户密码:


allUser中加入代码:

?? 2019/10/27 星期日 22:36:46

<form action="/findAll.do" method="post">
    <input name="searchname" type="text">
    <input type="submit" value="搜索">
</form>


id用户名密码操作
${user.id}${user.username}${user.password}删除| 修改
首页
    <a href="/findAll.do?pageNum=${pageInfo.pageNum-1}&size=5">上一页</a>

<c:forEach begin="1" end="${pageInfo.pages}" var="i">
    <a href="/findAll.do?pageNum=${i}&size=5">${i}</a>
</c:forEach>


    <a href="/findAll.do?pageNum=${pageInfo.pageNum+1}&size=5">下一页</a>


<a href="/findAll.do?pageNum=${pageInfo.pages}&size=5">尾页</a>

创建成功 失败 欢迎登录的jsp文件。
然后运行代码在网页中实现增删改查