12.SPRINGBOOT使用JPA实现完整的增删改查 CRUD和分页

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 = "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";
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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"intstart,@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";
    }
}
 步骤 6 : 

listCategory.jsp

通过page.getContent遍历当前页面的Category对象。
在分页的时候通过page.number获取当前页面,page.totalPages获取总页面数。
注:page.getContent会返回一个泛型是Category的集合。
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<%@ 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>
 步骤 7 : 

editCategory.jsp

修改分类的页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ 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>
 步骤 8 : 

重启测试

因为在pom中增加了新jar的依赖,所以要手动重启,重启后访问测试地址:
 
http://127.0.0.1:8080/listCategory?start=1
看到如图所示的效果
重启测试
x下载:  http://download.how2j.cn/1660/springboot.rar

posted on 2019-01-04 16:08  我是司  阅读(925)  评论(0)    收藏  举报

导航