在javaweb中使用分页查询的详细步骤

首先在原有的数据库实体bean上在建立一个分页实体bean

/**
* 用于展示分页数据的Javabean对象
* @author Lenovo
*
*/
public class PageenationBean {

private Integer currPage;//当前页数

private Integer totalPage;//总页数

private List<UserBean> dataList;//table的数据

public Integer getCurrPage() {
return currPage;
}

public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}

public Integer getTotalPage() {
return totalPage;
}

public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}

public List<UserBean> getDataList() {
return dataList;
}

public void setDataList(List<UserBean> dataList) {
this.dataList = dataList;
}
}

然后在持久层和业务层书写分页查询的方法

持久层:

public interface IUserDao {

/**
* 获取总条数
* @return
*/
public int getTotalCount();
/**
* 通过起始索引获取数据
* @param StartIndex
* @return
*/
public List<UserBean> getUserListByStartIndex(int StartIndex);

业务层:

public interface IUserService {

/**
* 获取总页数
* @return
*/
public int getTotalPage();

/**
* 通过起始索引获取数据
* @param StartIndex
* @return
*/
public List<UserBean> getUserListByCurrPage(int currPage);

然后实现接口方法

持久类接口实现:

public class UserDaoImpl extends BaseDao implements IUserDao{

@Override
public int getTotalCount() {
this.setConnection();
int totalCount = 0;
try {
ps = con.prepareStatement("select count(*) from stuscore");
rs = ps.executeQuery();
if(rs.next()) {
totalCount =rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
this.closeConnection();
return totalCount;
}

@Override
public List<UserBean> getUserListByStartIndex(int StartIndex) {
List<UserBean> list = new ArrayList<UserBean>();
this.setConnection();
try {
ps = con.prepareStatement("select * from stuscore limit ?,5");
ps.setInt(1, StartIndex);
rs=ps.executeQuery();
while(rs.next()) {
UserBean user=new UserBean();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setGrade(rs.getString("grade"));
user.setSex(rs.getString("sex"));
user.setAge(rs.getInt("age"));
user.setScore(rs.getInt("score"));
list.add(user);
}

} catch (Exception e) {
e.printStackTrace();
}finally {
this.closeConnection();
}
return list;
}

业务类接口实现:

public class UserServiceImpl implements IUserService{

private IUserDao dao= new UserDaoImpl();
@Override
public int getTotalPage() {
//得到数据总条数
int totalCount= dao.getTotalCount();
//计算总页数公式:totalPage = (totalCount + pageSize -1)/pageSize
int totalPage = (totalCount+5-1)/5;
return totalPage;
}

@Override
public List<UserBean> getUserListByCurrPage(int currPage) {
//通过当前页计算起始索引
int startIndex = (currPage-1)*5;
//通过起始索引查询数据
List<UserBean> userList =dao.getUserListByStartIndex(startIndex);
return userList;
}

然后 书写servlet方法

@WebServlet("/ShowUserListServlets")
public class ShowUserListServlets extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//动态获取当前第几页并做计算
String operation = request.getParameter("operation");
String currPageStr = request.getParameter("currPage");
int currPage = 1;
//调取业务层查询数据
IUserService service= new UserServiceImpl();
//获取总页数
int totalPage = service.getTotalPage();
if(currPageStr == null) {
currPage = 1;
}else if("首页".equals(operation)){
currPage = 1;
}else if("上一页".equals(operation)) {
currPage = Integer.parseInt(currPageStr)-1;
if(currPage <= 0) {
currPage = 1;
}
}else if("下一页".equals(operation)) {
currPage = Integer.parseInt(currPageStr)+1;
if(currPage >= totalPage) {
currPage = totalPage;
}
}

List<UserBean> userList = service.getUserListByCurrPage(currPage);

//构建javabean对象-PagenationBean
PageenationBean pagenationBean = new PageenationBean();
pagenationBean.setCurrPage(currPage);
pagenationBean.setTotalPage(totalPage);
pagenationBean.setDataList(userList);

//放入作用域
request.setAttribute("page", pagenationBean);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

最后用将jsp作为显示

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!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>
<table border="2" style="color:red,border-collapse: collapse;">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<c:forEach items="${page.dataList }" var="u">
<tr>
<td>${u.id }</td>
<td>${u.name }</td>
<td>${u.sex}</td>
<td>${u.age}</td>
</tr>
</c:forEach>
</table>
<h3>
第${page.currPage }页/共${page.totalPage }页
</h3>
<br>
<form action="ShowUserListServlets" method="get">
<input type="submit" value="首页" name="operation">
<input type="submit" value="上一页" name="operation">
<input type="submit" value="下一页" name="operation">
<input type="submit" value="尾页" name="operation">
<%--隐藏表单域作用,记录当前是第几页 --%>
<input type="hidden" name="currPage" value="${page.currPage }" >
<hr>
班级:<input type="text" name="class"><br>
性别:<input type="text" name="sex"><br>
成绩段:
<input type="text" name="min">至
<input type="text" name="max"><br>
<input type="submit" value="查询">
</form>
</body>
</html>

原文地址:https://www.cnblogs.com/wind-copy1234/p/11839378.html

时间: 2024-10-10 19:13:41

在javaweb中使用分页查询的详细步骤的相关文章

mongo中的分页查询

/** * @param $uid * @param $app_id * @param $start_time * @param $end_time * @param $start_page * @param $limit_page * mongodb中的分页查询 */ public static function getUserRevenueInfoList($uid, $source,$app_id, $start_time, $end_time,$skip, $limit){ $match

ssh框架中的分页查询

ssh中的分页查询是比较常用的,接下来我用代码来介绍如何实现一个分页查询 首先建立一个Model用来储存查询分页的信息 package com.haiziwang.qrlogin.utils; import java.util.List; public class prospage<T> { private int page; // 当前页数 private int totalCount; // 总记录数 private int totalPage; // 总页数 private int er

SQL Server中的分页查询 select top

SQL Server中的分页查询 https://blog.csdn.net/tswc_byy/article/details/82053091 零.码仙励志 比我差的人还没放弃,比我好的人仍在努力,我就更没资格说我无能为力 一.建库和建表 create database scort use scort create table emp ( empno int primary key, ename nvarchar(10), sal int, deptno int ) insert into e

unity3d开发的android应用中增加AD系统的详细步骤

unity3d开发的android应用中增加AD系统的详细步骤 博客分类: Unity3d unity3d Unity3d已经支持android,怎样在程序里增加admob? 试了一下,确实能够,只是必需要把androidproject的包名改成unitypackage里的包名. 决定自己也做一个unity3d的Plugins,记录一下步骤: 经过自己測试wozik提供的Plugins中的AndroidManifest.xml.res,libs.bin是必须的,其它东西都能够去掉. 依照这个思路

freecms中的分页查询

freecms支持分页查询,很多Service中都有用到,可以作为参考. 如果想支持分页查询,Action需要从BaseAction中派生出来,BaseAction中有order, currPage, pageSize等属性,在查询时传给Service,如 **Service.find(**, order, currPage,  pageSize).在Service的查询中,需要使用 mapper 的 selectPageByExample, 在 mapper中加入 selectPageByEx

怎样在 Akka Persistence 中实现分页查询

在 Akka Persistence 中,数据都缓存在服务内存(状态),后端存储的都是一些持久化的事件日志,没法使用类似 SQL 一样的 DSL 来进行分页查询.利用 Akka Streams 和 Actor 我们可以通过编码的方式来实现分页查询的效果,而且这个分页查询还是分步式并行的…… EventSourcedBehavior Akka Persistence的EventSourcedBehavior里实现了CQRS模型,通过commandHandler与eventHandler解耦了命令处

SQL Server中的分页查询

分页查询很简单,具体代码如下: --分页查询 --查询1-3行数据 select top 3 * from emp order by sal desc; --查询4-6行数据 select top 3 * from emp where empno not in (select top 3 empno from emp order by sal desc) order by sal desc; --查询7-9行数据 select top 3 * from emp where empno not i

Hibernate支持类中的分页查询的实现

Hibernate支持类的实现 package com.myHibernateDao; import java.sql.SQLException; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate

Java中oracle分页查询01

在数据库汇中的查询语句:oracle中使用rownum进行分页,从每页在总记录数的第n条到第m条:这里的是每页10条记录,下面sql所查询的是第页的记录数:oracle的分页是从1开始的,这里写成rownum>=0,rownum<=9,查询的记录是9条. SELECT * FROM (SELECT ROW_.*, ROWNUM ROWNUM_ FROM (SELECT * FROM TABLE1 WHERE TABLE1_ID = XX ORDER BY GMT_CREATE DESC) RO