12_springboot myBatis crud

1.pom.xml增加对PageHelper支持

<!--pagehelper支持-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
</dependency>

2.PageHelperConfig设置

package com.example.demo_zhang.config;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;

@Configuration
public class PageHelperConfig {
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

3.StudentController

@Controller
public class StudentController {
    @Autowired
    StudentMapper studentMapper;

    @RequestMapping("/listStudent")
    public String listStudent(Model m, @RequestParam(value = "start", defaultValue = "0") int start,
                              @RequestParam(value = "size", defaultValue = "5") int size)throws Exception{
        PageHelper.startPage(start, size, "stuid desc");
        List<Student> stu = studentMapper.findAll();
        PageInfo<Student> page = new PageInfo<>(stu);
        m.addAttribute("page",page);
        return "listStudent";
    }

    @RequestMapping("/addStudent")
    public String addStudent(Student s) throws Exception{
        studentMapper.save(s);
        return "redirect:listStudent";
    }

    @RequestMapping("/deleteStudent")
    public String deleteStudent(Student s) throws Exception {
        studentMapper.delete(s.getStuId());
        return "redirect:listStudent";
    }
    @RequestMapping("/updateStudent")
    public String updateStudent(Student s) throws Exception {
        studentMapper.update(s);
        return "redirect:listStudent";
    }

    @RequestMapping("/editStudent")
    public String editStudent(int stuId,Model m) throws Exception {
        Student s= studentMapper.get(stuId);
        m.addAttribute("s", s);
        return "editStudent";
    }
}

4.listStudent.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="500px;margin:20px auto;text-align: center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>stuId</td>
            <td>name</td>
            <td>sex</td>
            <td>编辑</td>
            <td>删除</td>
        </tr>
        <c:forEach items="${page.list}" var="s" varStatus="abc">
            <tr>
                <td>${s.stuId}</td>
                <td>${s.name}</td>
                <td>${s.sex}</td>
                <td><a href="editStudent?stuId=${s.stuId}">编辑</a></td>
                <td><a href="deleteStudent?stuId=${s.stuId}">删除</a></td>
            </tr>
        </c:forEach>

    </table>
    <br>
    <div>
        <a href="?start=1">[首  页]</a>
        <a href="?start=${page.pageNum-1}">[上一页]</a>
        <a href="?start=${page.pageNum+1}">[下一页]</a>
        <a href="?start=${page.pages}">[末  页]</a>
    </div>
    <br>
    <form action="addStudent" method="post">

        name: <input name="name"> <br>
        sex:  <input name="sex"><br>
        <button type="submit">提交</button>

    </form>
</div>

5.editStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>

<div style="margin:0px auto; 500px">

    <form action="/updateStudent" method="post">

        name: <input name="name" value="${s.name}"> <br>
        sex:  <input name="sex" value="${s.sex}"><br>

        <input name="stuId" type="hidden" value="${s.stuId}">
        <button type="submit">提交</button>

    </form>
</div>

6.运行http://127.0.0.1:8080/listStudent

image-20201111160806506
原文地址:https://www.cnblogs.com/NaoDaiYouDianDa/p/13992451.html