how2j.cn

下载区
文件名 文件大小
springboot.rar 6k
步骤 1 : CRUD和分页   
步骤 2 : 先运行,看到效果,再学习   
步骤 3 : 模仿和排错   
步骤 4 : 基于前面的知识点   
步骤 5 : CategoryController   
步骤 6 : listCategory.jsp   
步骤 7 : editCategory.jsp   
步骤 8 : 重启测试   

JPA 基本用法教程中 学习了JPA的基本运用,可是最后呢,总归还是要搞 CRUD和分页的。 并且借助CRUD和分页对JPA 的常用手法做一个学习。
步骤 2 :

先运行,看到效果,再学习

edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
访问测试地址:

http://127.0.0.1:8080/listCategory?start=1

注: 启动方式是Springboot特有的,直接运行类:com.how2java.springboot.Application 的主方法。
先运行,看到效果,再学习
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 4 :

基于前面的知识点

edit
本知识点,在Springboot JPA 基本用法的基础上进行
步骤 5 :

CategoryController

edit
为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 = "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";
}

1. 在参数里接受当前是第几页 start ,以及每页显示多少条数据 size。 默认值分别是0和5。

(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size)

2. 如果 start 为负,那么修改为0. 这个事情会发生在当前是首页,并点击了上一页的时候

start = start<0?0:start;

3. 设置倒排序

Sort sort = new Sort(Sort.Direction.DESC, "id");

4. 根据start,size和sort创建分页对象

Pageable pageable = new PageRequest(start, size, sort);

5. CategoryDAO根据这个分页对象获取结果page.

Page<Category> page =categoryDAO.findAll(pageable);

在这个page对象里,不仅包含了分页信息,还包含了数据信息,即有哪些分类数据。 这个可以通过getContent()获取出来。
6. 把page放在"page"属性里,跳转到listCategory.jsp

m.addAttribute("page", page);
return "listCategory";
package com.how2java.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.how2java.springboot.dao.CategoryDAO; import com.how2java.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 = "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"; } }
通过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>
修改分类的页面
<%@ 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>
<%@ 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

看到如图所示的效果
重启测试


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2023-10-27 新版本版本 springboot Sort 和 Pageable 的构造方法
rockmanzzx

Sort sort = Sort.by(Sort.Direction.DESC, "id"); Pageable pageable = PageRequest.of(start, size, sort);







回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2020-04-20 Sort sort = new Sort(Sort.Direction.DESC, "id"); 他报错了!!!
杵臼

Sort sort = new Sort(Sort.Direction.DESC, "id"); Pageable pageable = new PageRequest(start, size, sort); 他报错了!!! 改为 Sort sort = Sort.by(Sort.Direction.DESC, "id"); Pageable pageable = PageRequest.of(start, size, sort); 原因:我的spring boot版本太新了 参考博客:https://blog.csdn.net/qq_44039966/article/details/102713779




3 个答案

jh_315322
答案时间:2022-06-17
spring boot 2.7版本有效

苦练技术
答案时间:2021-04-13
亲测楼主的方法在Spring Boot的v2.3.7.RELEASE版本有效

a05572918
答案时间:2020-04-20
你是什么版本的springboot呀 我用站长的代码启动会因为1.5.9而不无法完成热部署导致出错 手动修改成2x版本以后application启动失败然后也无法访问网页怎么办



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2020-04-18 关于Pageable的解释在这里
2020-04-15 为什么一直404
2020-04-05 如何理解@RequestParam注解


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 19 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 框架-SpringBoot-JPA 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图