分页查询哦

本文介绍了一个基于Java Web的分页查询实现方案,包括了分页逻辑处理、DAO层的总记录数查询、Service层业务逻辑封装及JSP页面展示等内容。通过具体的代码示例展示了如何在Web应用中实现分页功能。

分页代码的实现

1.确定每页显示的数据数量

2.确定分页显示所需的总页数

3.编写SQL查询语句,实现数据查询

4.在JSP页面中进行分页显示设置

page实体类创建

public class Page {

    private Integer pageIndex;//页码

    private Integer pageSize;//页大小  显示多少行数据

    private Integer totalCounts;//数据的总行数

    private Integer totalPages;//总页数

    private Integer startRows;//起始行

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotalCounts() {
        return totalCounts;
    }

    public void setTotalCounts(Integer totalCounts) {
        this.totalCounts = totalCounts;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Integer getStartRows() {
        return startRows;
    }

    public void setStartRows(Integer startRows) {
        this.startRows = startRows;
    }
}

EmpPage实体类

public class EmpPage {
    private List<Employee> emps;
    private Page page;

    public List<Employee> getEmps() {
        return emps;
    }

    public void setEmps(List<Employee> emps) {
        this.emps = emps;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }
}

分页查询Dao层

总记录数

 

 

 service层

 

 

package com.qianfeng.controller;
import com.qianfeng.entity.EmpPage;
import com.qianfeng.entity.Page;
import com.qianfeng.service.EmpService;
import com.qianfeng.service.impl.EmpServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/emp/getAll")
public class EmpAllServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String index = req.getParameter("pageIndex");//获得第几页参数 页码

        int pageIndex;
        if(index==null){//用户访问的是首页 没有提交页码参数
            pageIndex=1;
        }else{
            pageIndex=Integer.parseInt(index);
        }

        Page page = new Page();
        page.setPageIndex(pageIndex);
        page.setPageSize(5);
        page.setStartRows((pageIndex-1)*page.getPageSize());


        EmpService empService = new EmpServiceImpl();

        EmpPage empPage = empService.getAll(page);

        req.setAttribute("emps",empPage.getEmps());
        req.setAttribute("page",empPage.getPage());
        //req.getRequestDispatcher("/showAll").forward(req,resp);
        req.getRequestDispatcher("/showAll.jsp").forward(req,resp);
    }
}
@WebServlet("/emp/update")
public class EmpUpdate  extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        int age = Integer.parseInt(req.getParameter("age"));
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String birthday = req.getParameter("birthday");

        Employee employee = new Employee();

        employee.setId(id);
        employee.setName(name);
        employee.setAge(age);
        employee.setGender(gender);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        employee.setBirthday(date);

        EmpService empService = new EmpServiceImpl();
        empService.update(employee);

        resp.sendRedirect("getAll");

    }
}

 

%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <table >
        <tr>
            <th>员工编号</th>
            <th>员工姓名</th>
            <th>员工年龄</th>
            <th>员工性别</th>
            <th>员工生日</th>
            <th>操作</th>
        </tr>

        <c:forEach items="${emps}" var="emp">

            <tr>
                <td>${emp.id}</td>
                <td>${emp.name}</td>
                <td>${emp.age}</td>
                <td>${emp.gender}</td>
                <td>${emp.birthday}</td>
                <td>
                    <button><a href="toUpdate?id=${emp.id}">编辑</a></button>
                    <button><a href="delete?id=${emp.id}">删除</a></button>
                </td>
            </tr>


        </c:forEach>



    </table>

    <a href="/empSysXin/add.html">添加</a>
    <a href="getAll?pageIndex=1">首页</a>
    <a href="getAll?pageIndex=${page.pageIndex-1<1?1:page.pageIndex-1}">上一页</a>
    <c:forEach  begin="1" end="${page.totalPages}" var="i">
        <a href="getAll?pageIndex=${i}">${i}</a>
    </c:forEach>
    <a href="getAll?pageIndex=${page.pageIndex+1>page.totalPages?page.totalPages:page.pageIndex+1}">下一页</a>
    <a href="getAll?pageIndex=${page.totalPages}">尾页</a>
</body>
</html>
%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="emp/update" method="post">
        <input name="id"  type="hidden" value="${emp.id}"><br/>
        <input name="name"  value="${emp.name}"><br/>
        <input name="age"  type="number"  value="${emp.age}"><br/>
        <input name="gender" value="${emp.gender}"><br/>
        <input name="birthday" type="date" value="<fmt:formatDate value="${emp.birthday}" pattern="yyyy-MM-dd" type="date" />" ><br/>
        <input type="submit" value="添加">
    </form>
</body>
</html>

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值