Springboot使用JPA实现完整的增删改查(CRUD)和分页
步骤 1 : 可运行项目
首先下载一个简单的可运行项目作为演示 -> 网盘链接:https://www.90pan.com/b1869093
下载后解压,比如解压到 E:\project\springboot 目录下
步骤 2 : CategoryController
为 CategoryController 添加: 增加、删除、获取、修改映射
@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";
}
值得注意:JPA 新增和修改用的都是 save. 它根据实体类的id是否为0来判断是进行增加还是修改
修改查询映射
@RequestMapping("/listCategory")
public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "6") 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";
}
-
在参数里接受当前是第几页 start ,以及每页显示多少条数据 size。 默认值分别是0和6。
(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "6") int size)
-
如果 start 为负,那么修改为0. 这个事情会发生在当前是首页,并点击了上一页的时候
start = start<0?0:start;
-
设置倒排序
Sort sort = new Sort(Sort.Direction.DESC, "id");
-
根据start,size和sort创建分页对象
Pageable pageable = new PageRequest(start, size, sort);
-
CategoryDAO根据这个分页对象获取结果page.
Page<Category> page = categoryDAO.findAll(pageable);
在这个 page 对象里,不仅包含了分页信息,还包含了数据信息,即有哪些分类数据。 这个可以通过 getContent() 获取出来。
- 把 page 放在 "page" 属性里,跳转到 listCategory.jsp
m.addAttribute("page", page);return "listCategory";
完整 CategoryController 类
package com.ryan.springboot.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ryan.springboot.dao.CategoryDAO;
import com.ryan.springboot.pojo.Category;
@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 = "6") 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";
}
}
步骤 3 : 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>
步骤 4 : 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>
步骤 5 : 重启测试
因为在pom中增加了新jar的依赖,所以要手动重启,重启后访问测试地址:
注: 启动方式是 Springboot 特有的,直接运行类:com.ryan.springboot.Application 的主方法。
看到如图所示的效果
更多关于 Springboot JPA使用 内容,点击学习: http://t.cn/A62l3XYr