分页显示所有信息的简单实现

具体实现效果如下所示:

首页超链接点击:

分页查询一对多关联去重Java java分页查询接口的实现_User


跳转到查询到的显示结果上面:

分页查询一对多关联去重Java java分页查询接口的实现_分页_02


点击下一页/尾页:

分页查询一对多关联去重Java java分页查询接口的实现_java_03


实现步骤:

1、准备工作

测试用的有个login表

分页查询一对多关联去重Java java分页查询接口的实现_servlet_04


项目具体分包如下:

分页查询一对多关联去重Java java分页查询接口的实现_分页_05


2、(bean层)创建表对应的实体类User和用于存储页面信息的Page类

User.java

package cn.hq.bean;

public class User {
	private String username;
	private String password;
	
	public User() {
		super();
	}

	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}
}

Page.java

package cn.hq.bean;

import java.util.List;

public class Page <T>{
	private List<T> list;		//当前页的学生列表
	private int totalSize;		//总的记录条数
	private int totalPage;		//总页面数
	private int currentPage;	//当前页
	private int pageSize;		//每页记录数
	public Page() {
		super();
	}
	public Page(List<T> list, int totalSize, int totalPage, int currentPage, int pageSize) {
		super();
		this.list = list;
		this.totalSize = totalSize;
		this.totalPage = totalPage;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public int getTotalSize() {
		return totalSize;
	}
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
}

3、UserDao,包含了要对表进行的相关操作(查询数据)

package cn.hq.dao;

import java.sql.SQLException;
import java.util.List;

import cn.hq.bean.User;

public interface UserDao {
	//定义一个每页显示的条数
	static int PAGE_SIZE=3;
	//根据当前页码去数据库查询对应的列表
	List<User> findUserByPage(int currentPage)throws SQLException;
	//查询数据库内总的记录条数
	int findTotalCount()throws SQLException;
}

4、Dao层的具体实现

package cn.hq.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.hq.bean.User;
import cn.hq.dao.UserDao;
import cn.hq.util.JDBCUtil;

public class UserDaoImpl implements UserDao {
	@Override
	public List<User> findUserByPage(int currentPage) throws SQLException {
		Connection conn = JDBCUtil.getConn();
		String sql="select * from login limit ?,?";
		PreparedStatement pre=conn.prepareStatement(sql);
		//这是从多少条数据开始读取
		pre.setInt(1, (currentPage-1)*PAGE_SIZE);
		//每页读取多少条记录
		pre.setInt(2, PAGE_SIZE);
		ResultSet rs=pre.executeQuery();
		List<User> list=new ArrayList<User>();
		while(rs.next()) {
			String name=rs.getString("username");
			String pass=rs.getString("password");
			User user=new User(name,pass);
			list.add(user);
		}
		JDBCUtil.close(conn, pre, rs);
		return list;
	}

	@Override
	public int findTotalCount() throws SQLException {
		Connection conn = JDBCUtil.getConn();
		String sql="select * from login";
		PreparedStatement pre=conn.prepareStatement(sql);
		ResultSet rs=pre.executeQuery();
		int count=0;
		while(rs.next()) {
			count++;
		}
		JDBCUtil.close(conn, pre, rs);
		return count;
	}
}

5、Service层

package cn.hq.service;

import java.sql.SQLException;
import java.util.List;

import cn.hq.bean.Page;

public interface UserService {
	//根据页码查出来的页面数据
	Page findUserByPage(int currentPage)throws SQLException;
}

Service的具体实现:

package cn.hq.service.impl;

import java.sql.SQLException;
import java.util.List;

import cn.hq.bean.Page;
import cn.hq.bean.User;
import cn.hq.dao.UserDao;
import cn.hq.dao.impl.UserDaoImpl;
import cn.hq.service.UserService;

public class UserServiceImpl implements UserService{

	@Override
	public Page findUserByPage(int currentPage) throws SQLException {
		//封装分页的数据
		Page<User> page=new Page<>();
		page.setCurrentPage(currentPage);		//设置当前页
		int pageSize=UserDao.PAGE_SIZE;
		page.setPageSize(pageSize);	//设置每页显示的记录数
		
		UserDao userDao=new UserDaoImpl();		//设置这页的用户数据
		List<User> list=userDao.findUserByPage(currentPage);
		page.setList(list);						//设置这一页要显示的用户数据
		
		int totalSize = userDao.findTotalCount();
		page.setTotalSize(totalSize); 			//设置总的记录数
		//设置总页数
		page.setTotalPage(totalSize%pageSize==0 ? totalSize/pageSize : (totalSize/pageSize+1));
		return page;
	}

}

6、Servlet层

package cn.hq.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

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 cn.hq.bean.Page;
import cn.hq.bean.User;
import cn.hq.service.UserService;
import cn.hq.service.impl.UserServiceImpl;


@WebServlet("/UserPageServlet")
public class UserPageServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//获取页面要显示的页码
			int currentPage = Integer.parseInt(request.getParameter("currentPage"));
			//去调用model层,查询该页对应的列表数据
			UserService service=new UserServiceImpl();
			Page page = service.findUserByPage(currentPage);
			//把获取的数据传给前端页面
			request.setAttribute("page", page);
			//页面跳转
			request.getRequestDispatcher("list_page.jsp").forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

7、最后是前端jsp页面:
首页:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<h3><a href="UserPageServlet?currentPage=1">分页显示所有学生</a></h3>
</body>
</html>

分页界面:list_page.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>
<table border="1px">
	<tr>
		<td>姓名</td>
		<td>密码</td>
		<td>操作</td>
	</tr>
	<c:forEach items="${page.list }" var="user">
		<tr>
			<td>${user.username }</td>
			<td>${user.password }</td>
			<td><a href="#">更新</a>    <a href="#">删除</a></td>
			
	    </tr>
	</c:forEach>
	<tr>
		<td colspan="3">
		第${page.currentPage }/${page.totalPage }
		    每页显示${page.pageSize }
		    总记录数${page.totalSize }    
		
		<c:if test="${page.currentPage!=1 }">
			<a href="UserPageServlet?currentPage=1">首页</a>|
			<a href="UserPageServlet?currentPage=${page.currentPage-1 }">上一页</a>
		</c:if>  
		
		<c:forEach begin="1" end="${page.totalPage }" var="i">
			<c:if test="${page.currentPage==i }">${i }</c:if>
			<c:if test="${page.currentPage!=i }">
				<a href="UserPageServlet?currentPage=${i }">${i }</a>
			</c:if>
		</c:forEach>  
		
		<c:if test="${page.currentPage!=page.totalPage }">
			<a href="UserPageServlet?currentPage=${page.currentPage+1 }">下一页</a>|
			<a href="UserPageServlet?currentPage=${page.totalPage }">尾页</a>
		</c:if>
		</td>
	</tr>
</table>
</body>
</html>

好像到这就基本完成了-_-