使用ssm实现分页查询。

创建分页工具类Page


import java.io.Serializable;

public class Page implements Serializable {
    private static final long serialVersionUID=1L;

    private int pageNow=1;      //当前页数

    private int pageSize=2;     //每页显示的数量

    private int totalCount;     //总记录数

    private int totalPageCount;      //总页数

    private int startPos;        //开始位置,从0开始

    private boolean hasFrist;      //是否有首页

    private boolean hasPre;     //是否有前一页

    private boolean hasNext;     //是否有后一页

    private boolean hasLast;     //是否有尾页

    /**
     * 通过构造函数,传入总记录数和当前页
     * @param pageNow
     * @param totalCount
     */
    public Page(int pageNow, int totalCount) {
        super();
        this.pageNow = pageNow;
        this.totalCount = totalCount;
    }
    //获取总页数
    public int getTotalPageCount() {
        totalPageCount=getTotalCount()/getPageSize();
        return (totalCount/pageSize==0) ? totalPageCount:totalPageCount+1;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    //取得选择记录的初始位置
    public int getStartPos() {
        return (pageNow-1)*pageSize;
    }

    public void setStartPos(int startPos) {
        this.startPos = startPos;
    }
    //判断是否有第一页
    public boolean isHasFrist() {
        return (pageNow==1) ? false:true;
    }

    public void setHasFrist(boolean hasFrist) {
        this.hasFrist = hasFrist;
    }
    //如果有首页就有前一页
    public boolean isHasPre() {
        return isHasFrist() ? true:false;
    }

    public void setHasPre(boolean hasPre) {
        this.hasPre = hasPre;
    }
    //如果有尾页就有下一页
    public boolean isHasNext() {
        return isHasNext() ? true:false;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }
    //判断是否有尾页
    public boolean isHasLast() {
        return (pageNow==getTotalCount()) ? false:true;
    }

    public void setHasLast(boolean hasLast) {
        this.hasLast = hasLast;
    }
}

mapper接口


import cn.ali.entity.Student;

import java.util.List;

public interface StudentMapper
{
	/**
	 * 查询分页数据
	 * @param pageNow 
	 * @param pageSize
	 * @return
	 */
	List<Student> searchStudent(Integer pageNow,Integer pageSize);

	/**
	 * 查询总页数
	 * @return
	 */
	Integer searchStudentCount();

}

mapper映射文件

<!-- sql语句 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ali.dao.StudentMapper">
	<!--分页查询-->
	<select id="searchStudent" resultType="cn.ali.entity.Student">
		select * from student,classes WHERE student.classid=classes.id
		limit #{0},#{1}
	</select>
	
	<!--总页数-->
	<select id="searchStudentCount" resultType="java.lang.Integer">
		select  COUNT(*) from student
	</select>
	
</mapper>

controller类


import cn.ali.entity.Classes;
import cn.ali.entity.Student;
import cn.ali.service.ClassesService;
import cn.ali.service.StudentService;
import cn.ali.utils.Page;
import cn.ali.utils.SpringUtils;
import com.alibaba.fastjson.JSON;
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 org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class SearchController 
{

	@Autowired
	private StudentService studentService;

	@RequestMapping("/SearchStudent")
	public String SearchBug_detail(Model model, HttpServletRequest request){
		//获取当前页数
		String pageNow=request.getParameter("pageNow");
		//获取总页数
		int totalCount=(int)studentService.searchStudentCount();
		Page page=null;
		List<Student> list=new ArrayList<Student>();
		if (pageNow!=null) {
			page=new Page(Integer.parseInt(pageNow), totalCount);
			list=this.studentService.searchStudent(page.getStartPos(),page.getPageSize());
		}else {
			page=new Page(1, totalCount);
			list=this.studentService.searchStudent(page.getStartPos(),page.getPageSize());
		}
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		return "/index";
	}

}

显示页面jsp

  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2018/10/16
  Time: 8:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<script src="../../jquery-3.2.1.js" type="text/javascript"></script>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <table id="test" style="position: relative; left: 30%">

    <tr style="color: black" >
      <td>学员信息列表</td>
    </tr>
    <tr bgcolor="#8fbc8f" style="color: black">
      <td>编号</td>
      <td width="60px">姓名</td>
      <td width="60px">性别</td>
      <td width="60px">生日</td>
      <td width="60px">电话</td>
      <td width="60px">E-mail</td>
      <td width="60px">班级</td>
    </tr>
  <c:forEach items="${list}" var="p">
    <tr bgcolor="#8fbc8f" style="color: black">
      <td><a href="/updateStudent?id=${p.id}">${p.id}</a> </td>
      <td width="60px">${p.sname}</td>
      <td width="60px">${p.birthday}</td>
      <td width="60px">${p.gender}</td>
      <td width="60px">${p.telephone}</td>
      <td width="60px">${p.email}</td>
      <td width="60px">${p.cname}</td>
    </tr>
  </c:forEach>

  </table>

  <div class="pager">
  <font size="2">共 ${page.totalPageCount} 页</font> <font size="2">第
    ${page.pageNow} 页</font> <a href="/SearchStudent?pageNow=1">首页</a>
  <c:choose>
    <c:when test="${page.pageNow - 1 > 0}">
      <a href="/SearchStudent?pageNow=${page.pageNow - 1}">上一页</a>
    </c:when>
    <c:when test="${page.pageNow - 1 <= 0}">
      <a href="/SearchStudent?pageNow=1">上一页</a>
    </c:when>
  </c:choose>
  <c:choose>
    <c:when test="${page.totalPageCount==0}">
      <a href="/SearchStudent?pageNow=${page.pageNow}">下一页</a>
    </c:when>
    <c:when test="${page.pageNow + 1 < page.totalPageCount}">
      <a href="/SearchStudent?pageNow=${page.pageNow + 1}">下一页</a>
    </c:when>
    <c:when test="${page.pageNow + 1 >= page.totalPageCount}">
      <a href="/SearchStudent?pageNow=${page.totalPageCount}">下一页</a>
    </c:when>
  </c:choose>
  <c:choose>
    <c:when test="${page.totalPageCount==0}">
      <a href="/SearchStudent?pageNow=${page.pageNow}">尾页</a>
    </c:when>
    <c:otherwise>
      <a href="/SearchStudent?pageNow=${page.totalPageCount}">尾页</a>
    </c:otherwise>
  </c:choose>
  </div>

  </body>
</html>

需要注意页面显示需要用到jstl,记得引入jar文件

猜你喜欢

转载自blog.csdn.net/qq_41035400/article/details/83211337