当数据量较多时,用户需要拖动页面才可以浏览更多消息
分页显示的步骤(思路)
- 确定每页显示的数据量
- 确定分页显示所需的总页面
- 编写SQL查询语句,实现数据查询
- 在JSP页面中进行分页显示设置
一:计算显示总页数:
(1)select count(1)from 表名
效率高于
(2)select count(*)from 表名
第一种方式:直接获取查询的数字
第二种方式:首先遍历表,再把数据替换成* 在查询有多少条数据
二:根据每页显示记录数计算出总页数:
totalPageCount=(this.totalCount%pageSize==0)?(this.totalCount/pageSize):((this.totalCount/pageSize)+1);
三:使用LIMIT子句(分页SQL):
LIMIT 0 , 3
0::起始下标=(当前页页码-1)*每页显示的条数
3::每页显示的数量
代码实现:
代码编写步骤及思路
1.导入jar包
2.创建util包 BaseDao
3.创建实体类pojo
4.在util包中创建一个工具类 page
当前页 currentPage
页面容量 pageSize
数据总数(sql查询)totalCount
总页数 totalCountPageCount (totalCount和pageSize计算出来)
每页数据对象集合 List<User>
5.数据访问层(dao层)
(1)获取数据总量(返回值int)
(2)获取每页数据对象集合(List<User> 参数:当前页,页面容量)
《开始下标=(当前页-1)*页面容量)》
6.业务逻辑层(service)
组装page工具类
赋值
7.servlet
调用业务逻辑层,向页面传递参数 page对象和每次的List集合
8.jsp
编写页面 接收servlet传入的参数
循环遍历集合
根据当前页数判断执行不同的页面结果
超链接进入servlet同时传入当前的当前页数
、代码实现:
在util包中创建一个工具类 page:
package com.bdqn.util;
import com.bdqn.pojo.User;
import java.util.List;
//分页工具类
public class UserPage {
//当前页数(从前台传入)
private int currentPage;
//页面容量(3)
private int pageSize;
//数据总数(sql查询)
private int totalCount;
//总页数(计算 :数据总数/页面容量 )
private int totalPageCount;
//当前展示的数据集合(sql查询)
private List<User> userList;
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;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
if(this.totalCount%this.pageSize==0){
//总页数=数据总数/页面容量
this.totalPageCount=this.totalCount/this.pageSize;
}else{
this.totalPageCount=this.totalCount/this.pageSize+1;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
5.数据访问层(dao层):
public interface UserDao {
//查询数据总量
int getUserCount();
//查询当前页面的数据
List<User> getUserList(int currentPage,int pageSize);
}
5.1:dao 实现类impl:
public class UserDaoImpl extends BaseDao implements UserDao {
@Override
public int getUserCount() {
String sql="SELECT COUNT(1) as c FROM USER";
ResultSet rs = this.executeQuery(sql, null);
int count=0;
try {
if(rs.next()){
count=rs.getInt("c");
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
@Override
public List<User> getUserList(int currentPage, int pageSize) {
String sql="SELECT * FROM USER AS u,usertype AS t WHERE u.typeId=t.typeId LIMIT ?,?";
Object [] objects={currentPage*pageSize,pageSize};
ResultSet rs = this.executeQuery(sql, objects);
List<User> userList=new ArrayList<>();
try {
while(rs.next()){
User user=new User();
UserType ut=new UserType();//创建类型对象
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("userName"));
user.setUserPwd(rs.getString("userPwd"));
user.setAddress(rs.getString("address"));
ut.setTypeName(rs.getString("typeName"));//给类型对象赋值
user.setUserType(ut);//给用户对象的类型赋值
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
}
我们这次进行链表查 两个表:
5.1.1:第一个表的实体类:
public class User {
private int id;
private String userName;
private String userPwd;
private String address;
private int typeId;
//添加类型
private UserType userType;
//-------------添加get/set方法---------------//
}
5.1.2:第二个表的实体类:
public class UserType {
private int typeId;
private String typeName;
//----------添加get/set方法-------------//
}
6.业务逻辑层(service):
public interface UserService {
//组装page工具类
UserPage getUsers(int currentPage,int pageSize);
}
6.1:service的实现类impl:
public class UserServiceImpl implements UserService {
//创建数据访问层
UserDao ud=new UserDaoImpl();
@Override
public UserPage getUsers(int currentPage, int pageSize) {
UserPage up=new UserPage();
up.setPageSize(pageSize);
up.setTotalCount(ud.getUserCount());
up.setCurrentPage(currentPage);
up.setUserList(ud.getUserList(currentPage,pageSize));
return up;
}
}
7.servlet:
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
UserService us=new UserServiceImpl();
req.setCharacterEncoding("utf-8");
HttpSession session=req.getSession();
//从前台传入一个当前页数
String curr = req.getParameter("curr");
//如果是第一次,从第一条开始查询
if(curr==null){
curr="0";
}
int currentPage = Integer.valueOf(curr);
UserPage up = us.getUsers(currentPage, 3);
List<User> userList = up.getUserList();
session.setAttribute("up",up);
session.setAttribute("uList",userList);
resp.sendRedirect("index.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
8.jsp:
<body>
<%
UserPage up=(UserPage)session.getAttribute("up");
List<User> userList=(List<User>)session.getAttribute("uList");
%>
<table align="center" border="1px">
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
<td>地址</td>
<td>身份</td>
</tr>
<%
for (User user:userList){
%>
<tr>
<td><%=user.getId()%>
</td>
<td><%=user.getUserName()%>
</td>
<td><%=user.getUserPwd()%>
</td>
<td><%=user.getAddress()%>
</td>
<td><%=user.getUserType().getTypeName()%></td>
</tr>
<%
}
%>
<tr>
<td colspan="5">
<%
if(up.getCurrentPage()==0){
%>
<a href="UserServlet?curr=<%=up.getCurrentPage()+1%>">下一页</a>
<a href="UserServlet?curr=<%=up.getTotalPageCount()-1%>">尾页</a>
<%
}else if(up.getCurrentPage()>0 && up.getCurrentPage()+1!=up.getTotalPageCount()){
%>
<a href="UserServlet?curr=0">首页</a>
<a href="UserServlet?curr=<%=up.getCurrentPage()-1%>">上一页</a>
<a href="UserServlet?curr=<%=up.getCurrentPage()+1%>">下一页</a>
<a href="UserServlet?curr=<%=up.getTotalPageCount()-1%>">尾页</a>
<%
}else if(up.getCurrentPage()+1==up.getTotalPageCount()){
%>
<a href="UserServlet?curr=0">首页</a>
<a href="UserServlet?curr=<%=up.getCurrentPage()-1%>">上一页</a>
<%
}
%>
</td>
</tr>
</table>
</body>