springBoot+jpa的增删差改操作(CRUD)+mysq8.0
1. 基于之前
如果没有接触过JPA请先移步springBoot+JPA+mysql8.0.15快速入门教程
2.修改CategoryController
为CategoryController添加: 增加、删除、获取、修改映射
package com.eknaij.springbootjpa.controller;
import com.eknaij.springbootjpa.dao.CategoryDAO;
import com.eknaij.springbootjpa.pojo.Category;
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 java.util.List;
@Controller
public class CategoryController {
@Autowired
CategoryDAO categoryDAO;
//查询所有
@RequestMapping("/listCategory")
//在参数里接受当前是第几页 start ,以及每页显示多少条数据 size。 默认值分别是0和5
public String listCategory(Model model, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
start = start<0?0:start; //如果 start 为负,那么修改为0. 这样在首页点击上一页的时候就会停留在首页
//设置排序方式,ASC是顺序,DESC是倒序
Sort sort = new Sort(Sort.Direction.ASC,"id");
Pageable pageable = new PageRequest(start, size, sort);
Page<Category> page =categoryDAO.findAll(pageable);
model.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 editCategory(int id,Model model) throws Exception {
Category c= categoryDAO.getOne(id);
model.addAttribute("c", c);
return "editCategory";
}
}
3.修改listCategory.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>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.添加一个用于编辑信息的页面
<%@ 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.启动测试
浏览器中输入http://localhost:8080/listCategory
就可以看到效果了