欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ssm-分页查询

程序员文章站 2024-03-14 20:03:23
...

第一步:导入maven依赖

 <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.11</version>
        </dependency>

第二步:在mybatis-config中配置
必须写在 之后

 <!--分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

第三步:写一个分页的工具类

package com.aaa.until;

import com.aaa.pojo.Emp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpServletRequest;

public class PageUntil {
    //分页
    public  static  String getPageInfo(PageInfo<Emp> pageInfo, HttpServletRequest request){
       StringBuilder builder=new StringBuilder();
       //获取项目路径
        String path = request.getContextPath()+"/";
        //拼接首页
        builder.append("<a href='"+path+"fen/1'>首页</a>");
        builder.append("&nbsp;&nbsp;");
        //判断上一页 如果时第一页则显示当前页
        //isHasPreviousPage意思时是否有前一页,
        //如果有前一页就显示前一页
        if (pageInfo.isHasPreviousPage()){
            builder.append("<a href='"+path+"fen/"+pageInfo.getPrePage()+"'>上一页</a>");
            builder.append("&nbsp;&nbsp;");
        }else{
            builder.append("上一页");
            builder.append("&nbsp;&nbsp;");
        }
        //拼接页码[导航条] [1] [2]
        int[] nf = pageInfo.getNavigatepageNums();
        for (int i : nf) {
            if (i==pageInfo.getPageNum()){
                builder.append("<a style='color:red;' href='"+path+"fen/"+i+"'>"+i+"</a>");
                builder.append("&nbsp;&nbsp;");
            }
            else{
                builder.append("<a href='"+path+"fen/"+i+"'>"+i+"</a>");
                builder.append("&nbsp;&nbsp;");
            }

        }
        //拼接下一页
        if (pageInfo.isHasNextPage()){
            builder.append("<a href='"+path+"fen/"+pageInfo.getNextPage()+"'>下一页</a>");
            builder.append("&nbsp;&nbsp;");
        }else{
            builder.append("下一页");
            builder.append("&nbsp;&nbsp;");
        }
        //拼接尾页
        builder.append("<a href='"+path+"fen/"+pageInfo.getPages()+"'>尾页</a>");
        builder.append("&nbsp;&nbsp;");
        return builder.toString();

    }
}

第四步:在控制器层中编写代码

package com.aaa.controller;

import com.aaa.pojo.Emp;
import com.aaa.service.EmpService;
import com.aaa.until.PageUntil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.EnumMap;
import java.util.List;

@Controller
public class EmpController {
    @Autowired
    private EmpService empService;
    //查询
@RequestMapping("/fen/{pageNum}")
public String fenYe(Model model, @PathVariable("pageNum") Integer pageNum, HttpServletRequest request){
    // pageNum 第几页    pageSize:每页显示几条数据
    PageHelper.startPage(pageNum,2);
    List<Emp> emps = empService.queryEmp();
    PageInfo<Emp> pageInfo=new PageInfo<Emp>(emps,5);
    String pageInfo1 = PageUntil.getPageInfo(pageInfo, request);
    model.addAttribute("emps",emps);
    model.addAttribute("pageInfo1",pageInfo1);
    return  "list";
}

}

第五步:编写list.jsp 页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>分页查询</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aaa@qq.com/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-bordered table-hover">
                <thead>
                <tr class="info">
                    <th>编号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>部门</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${emps}" var="list">
                    <tr>
                        <td  class="active">${list.eid}</td>
                        <td class="success">${list.ename}</td>
                        <td class="warning">${list.sex==0?'男':'女'}</td>
                        <td class="danger">${list.deptss.dname}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}//${list.eid}">删除</a>
                            <a href="">修改</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="5" style="text-align: center">${pageInfo1}</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>


</body>
</html>

效果图如下:
ssm-分页查询

相关标签: ssm