SpringBoot-使用JPA實現完整的CRUD和分頁
阿新 • • 發佈:2019-01-08
- CRUD和分頁
在 JPA 基本用法教程中 學習了JPA的基本運用,可是最後呢,總歸還是要搞 CRUD和分頁的。 並且藉助CRUD和分頁對JPA 的常用手法做一個學習。 - 基於前面的知識點
本知識點,在Springboot JPA 基本用法的基礎上進行 - CategoryController
為CategoryController新增: 增加、刪除、獲取、修改對映
值得注意:JPA 新增和修改用的都是save. 它根據實體類的id是否為0來判斷是進行增加還是修改@RequestMapping("/addCategory") public String addCategory(Category c) throws Exception { categoryDAO.save(c); return "redirect:listCategory"; } @RequestMapping("/deleteCategory") public String deleteCategory(Category c) throws Exception { categoryDAO.delete(c); return "redirect:listCategory"; } @RequestMapping("/updateCategory") public String updateCategory(Category c) throws Exception { categoryDAO.save(c); return "redirect:listCategory"; } @RequestMapping("/editCategory") public String editCategory(int id,Model m) throws Exception { Category c= categoryDAO.getOne(id); m.addAttribute("c", c); return "editCategory"; }
修改查詢對映
1. 在引數裡接受當前是第幾頁 start ,以及每頁顯示多少條資料 size。 預設值分別是0和5。@RequestMapping("/listCategory") public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception { start = start<0?0:start; Sort sort = new Sort(Sort.Direction.DESC, "id"); Pageable pageable = new PageRequest(start, size, sort); Page<Category> page =categoryDAO.findAll(pageable); m.addAttribute("page", page); return "listCategory"; }
2. 如果 start 為負,那麼修改為0. 這個事情會發生在當前是首頁,並點選了上一頁的時候(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size)
3. 設定倒排序start = start<0?0:start;
4. 根據start,size和sort建立分頁物件Sort sort = new Sort(Sort.Direction.DESC, "id");
5. CategoryDAO根據這個分頁物件獲取結果page.Pageable pageable = new PageRequest(start, size, sort);
在這個page物件裡,不僅包含了分頁資訊,還包含了資料資訊,即有哪些分類資料。 這個可以通過getContent()獲取出來。Page<Category> page =categoryDAO.findAll(pageable);
6. 把page放在"page"屬性裡,跳轉到listCategory.jspm.addAttribute("page", page); return "listCategory";
@Controller public class CategoryController { @Autowired CategoryDAO categoryDAO; @RequestMapping("/listCategory") public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception { start = start<0?0:start; Sort sort = new Sort(Sort.Direction.DESC, "id"); Pageable pageable = new PageRequest(start, size, sort); Page<Category> page =categoryDAO.findAll(pageable); System.out.println(page.getNumber()); System.out.println(page.getNumberOfElements()); System.out.println(page.getSize()); System.out.println(page.getTotalElements()); System.out.println(page.getTotalPages()); m.addAttribute("page", page); return "listCategory"; } @RequestMapping("/addCategory") public String addCategory(Category c) throws Exception { categoryDAO.save(c); return "redirect:listCategory"; } @RequestMapping("/deleteCategory") public String deleteCategory(Category c) throws Exception { categoryDAO.delete(c); return "redirect:listCategory"; } @RequestMapping("/updateCategory") public String updateCategory(Category c) throws Exception { categoryDAO.save(c); return "redirect:listCategory"; } @RequestMapping("/editCategory") public String ediitCategory(int id,Model m) throws Exception { Category c= categoryDAO.getOne(id); m.addAttribute("c", c); return "editCategory"; } }
- listCategory.jsp
通過page.getContent遍歷當前頁面的Category物件。
在分頁的時候通過page.number獲取當前頁面,page.totalPages獲取總頁面數。
注:page.getContent會返回一個泛型是Category的集合。<%@ 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>id</td> <td>name</td> <td>編輯</td> <td>刪除</td> </tr> <c:forEach items="${page.content}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> <td><a href="editCategory?id=${c.id}">編輯</a></td> <td><a href="deleteCategory?id=${c.id}">刪除</a></td> </tr> </c:forEach> </table> <br> <div> <a href="?start=0">[首 頁]</a> <a href="?start=${page.number-1}">[上一頁]</a> <a href="?start=${page.number+1}">[下一頁]</a> <a href="?start=${page.totalPages-1}">[末 頁]</a> </div> <br> <form action="addCategory" method="post"> name: <input name="name"> <br> <button type="submit">提交</button> </form> </div>
- editCategory.jsp
修改分類的頁面<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <div style="margin:0px auto; width:500px"> <form action="updateCategory" method="post"> name: <input name="name" value="${c.name}"> <br> <input name="id" type="hidden" value="${c.id}"> <button type="submit">提交</button> </form> </div>
- 重啟測試
因為在pom中增加了新jar的依賴,所以要手動重啟,重啟後訪問測試地址:
看到如圖所示的效果http://127.0.0.1:8080/listCategory?start=1