12_springboot myBatis crud
阿新 • • 發佈:2020-11-30
1.pom.xml增加對PageHelper支援
<!--pagehelper支援-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
2.PageHelperConfig設定
package com.example.demo_zhang.config; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.PageHelper; @Configuration public class PageHelperConfig { @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; } }
3.StudentController
@Controller public class StudentController { @Autowired StudentMapper studentMapper; @RequestMapping("/listStudent") public String listStudent(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size)throws Exception{ PageHelper.startPage(start, size, "stuid desc"); List<Student> stu = studentMapper.findAll(); PageInfo<Student> page = new PageInfo<>(stu); m.addAttribute("page",page); return "listStudent"; } @RequestMapping("/addStudent") public String addStudent(Student s) throws Exception{ studentMapper.save(s); return "redirect:listStudent"; } @RequestMapping("/deleteStudent") public String deleteStudent(Student s) throws Exception { studentMapper.delete(s.getStuId()); return "redirect:listStudent"; } @RequestMapping("/updateStudent") public String updateStudent(Student s) throws Exception { studentMapper.update(s); return "redirect:listStudent"; } @RequestMapping("/editStudent") public String editStudent(int stuId,Model m) throws Exception { Student s= studentMapper.get(stuId); m.addAttribute("s", s); return "editStudent"; } }
4.listStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <div align="center"> </div> <div style="width:500px;margin:20px auto;text-align: center"> <table align='center' border='1' cellspacing='0'> <tr> <td>stuId</td> <td>name</td> <td>sex</td> <td>編輯</td> <td>刪除</td> </tr> <c:forEach items="${page.list}" var="s" varStatus="abc"> <tr> <td>${s.stuId}</td> <td>${s.name}</td> <td>${s.sex}</td> <td><a href="editStudent?stuId=${s.stuId}">編輯</a></td> <td><a href="deleteStudent?stuId=${s.stuId}">刪除</a></td> </tr> </c:forEach> </table> <br> <div> <a href="?start=1">[首 頁]</a> <a href="?start=${page.pageNum-1}">[上一頁]</a> <a href="?start=${page.pageNum+1}">[下一頁]</a> <a href="?start=${page.pages}">[末 頁]</a> </div> <br> <form action="addStudent" method="post"> name: <input name="name"> <br> sex: <input name="sex"><br> <button type="submit">提交</button> </form> </div>
5.editStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<div style="margin:0px auto; width:500px">
<form action="/updateStudent" method="post">
name: <input name="name" value="${s.name}"> <br>
sex: <input name="sex" value="${s.sex}"><br>
<input name="stuId" type="hidden" value="${s.stuId}">
<button type="submit">提交</button>
</form>
</div>
6.執行http://127.0.0.1:8080/listStudent