插件-pageHelper

1、引入jar包:
pagehelper-5.0.3.jar
jsqlparser-0.9.5.jar
2、编写配置:
mybatis-config.xml

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

3、写代码
TeacherController.java

package com.atguigu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.atguigu.bean.Teacher;
import com.atguigu.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class TeacherController {
    
    @Autowired
    TeacherService teacherService;
    
    @RequestMapping("/getall")
    public String getAll(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
        //紧跟他的查询就是一个分页查询
        PageHelper.startPage(pn, 5);
        List<Teacher> list = teacherService.getAll();
        
        //我们可以将查询的结果使用;将查询的结果放在pageinfo中这个pageInfo就有非常多能够用的
        //第二份传入连续要显示的页码
        PageInfo<Teacher> info = new PageInfo<>(list, 6);
        System.out.println("当前页码:"+info.getPageNum());
        System.out.println("总页码:"+info.getPages());
        System.out.println("总记录数:"+info.getTotal());
        System.out.println("当前页有几条记录:"+info.getSize());
        System.out.println("当前页的pageSize:"+info.getPageSize());
        System.out.println("前一页:"+info.getPrePage());
        System.out.println("结果:"+info.getList());//查询结果
        int[] nums = info.getNavigatepageNums();
        
        model.addAttribute("info", info);
        return "success";
    }

}

TeacherService.java

    @Autowired
    private TeacherDao teacherDao;

    public List<Teacher> getAll() {
        
        return teacherDao.getTeachers();
    }

TeacherDao.xml

    <!--public List<Teacher> getTeachers();  -->
    <select id="getTeachers" resultMap="teacherMap">
        select * from t_teacher
    </select>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>成功!</h1>
    <table cellpadding="5" cellspacing="0" border="1">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>course</th>
            <th>address</th>
        </tr>
        <c:forEach items="${info.list}" var="tea">
            <tr>
                <td>${tea.id }</td>
                <td>${tea.name }</td>
                <td>${tea.course }</td>
                <td>宝安区</td>
            </tr>
        </c:forEach>
        <tr>
            <td colspan="4">
                <a href="getall?pn=1">首页</a><a href="getall?pn=${info.prePage }">上一页</a> 
                
                <c:forEach items="${info.navigatepageNums}" var="num">
                    <c:if test="${num == info.pageNum }">
                            【${num }】
                    </c:if>
                    <c:if test="${num != info.pageNum }">
                            <a href="getall?pn=${num }">${num }</a>
                    </c:if>
                </c:forEach>
                
                <a href="getall?pn=${info.nextPage }">下一页</a><a href="getall?pn=${info.pages }">末页</a>
            
            </td>
        </tr>
    </table>
    
</body>
</html>

这里用了标签<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。