zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Android WebView 获取网页的标题
    Android APK 文件自动安装
    DownloadManager 的使用
    Android Java 自定义异常
    Android NDK
    android assets文件夹资源的访问
    GitHub 基本常用知识解答2
    hbuilder egit插件的安装使用--项目文件丢失的教训
    微软收购跨平台移动应用开发商Xamarin
    Android Study 之 初识ButterKnife(8.5.1)及简单运用
  • 原文地址:https://www.cnblogs.com/NaoDaiYouDianDa/p/13992451.html
Copyright © 2011-2022 走看看