一,为什么要采用分页技术
当数据量较多,页面显示不完全时,不需要用户拖动页面也能浏览更多信息
二,分页显示的步骤
- 确定每页显示的数据数量
- 计算显示的页数
- 编写SQL语句
三,行数与页数的关系
int page=1 --当前的页数
int rows=2 --每页显示的行数
page:1 【1(begin开始位置),5(end结束位置)】
page:2 【5,10】
page:3 【10,15】
//找规律:
begin 开始行数: (page-1)*rows+1
end 结束行数: page*rows
四,sql语句的编写
我们需要把数据库中的数据按照固定的数量分成不同的页数,再让其显示,这个时候很多的朋友就会想,应该用什么来作为分页的依据呢,这个时候就要用到今天的新知识:伪列(rownum)
当我们使用rownum时,表格中就会出现一列名叫rownum的数据,从1开始,给表格中的数据逐一给了一个伪id ,这时候我们就可以使用它来完成数据分页的操作。如果使用表格中数据自带的id的话,就会出现一种情况,当数据被删除时,该条数据所处的那一页就会少了一条数据,而用rownum就不会出现这样的情况,不管是否有数据被删除,都不会影响rownum。
select a.*,ROWNUM from t_news a where news_title like ?
使用rownum的注意事项
rownum不能够用于>1的情况
between也要从1开始
rownum不能进行排序的操作
由于rownum不能进行排序的操作,所以我们需要把rownum(伪列) 变成表格中的实列。也就是先查询出有伪列的数据结果,然后将这个结果变成新的表格数据。
select * from (" select a.*,ROWNUM myr from t_news a where news_title like ?") b where myr between ? and ?
dao包代码:
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.pojo.News;
import com.zking.util.DBHelper;
public class NewsDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 根据新闻名称进行模糊查询
* @param newName
* @param page 让别人传入的页面数
* @return
*/
public List<News> queryByName(String newName,int page) {
int rows=5;//条数
int begin=1+((page-1)*rows);//开始位置
int end=page*rows;//结束位置
List<News> list = new ArrayList<News>();
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from (" +
" select a.*,ROWNUM myr from t_news a where news_title like ?" +
") b where myr between ? and ?");
ps.setString(1, "%" + newName + "%");
ps.setInt(2, begin);
ps.setInt(3, end);
rs = ps.executeQuery();
while (rs.next()) {
News news = new News();
// 給新聞对象赋值
news.setNewsId(rs.getInt(1));
news.setNewsTitle(rs.getString(2));
news.setNewsTopic(rs.getInt(3));
news.setNewsAuthor(rs.getString(4));
news.setNewsPublisher(rs.getString(5));
news.setNewsContent(rs.getString(6));
news.setNewsCover(rs.getString(7));
news.setNewsCount(rs.getInt(8));
news.setNewsMarker(rs.getInt(9));
// 将新闻对象添加到集合中
list.add(news);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.close(con, ps, rs);
}
return list;
}
//怎么知道当前有几页
//数据库的数量/条数
/**
* 查询当前与传入名称相应的总页数
* @param newName
* @return
*/
public int queryCount(String newName) {
int rows=5;//条数
int count=0;//数据库里面数据的数量
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select count(1) from t_news where news_title like ? ");
ps.setString(1, "%" + newName + "%");
rs = ps.executeQuery();
if (rs.next()) {
count= rs.getInt(1);//数据库的条数
}
//返回页数13.0/5=2.x->3.0
// return Integer.parseInt(Math.ceil(count*1.0/rows)+"");//求整
return new Double(Math.ceil(count*1.0/rows)).intValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.close(con, ps, rs);
}
return 0;
}
}
index.jsp(首页代码)
<%@page import="com.zking.pojo.News"%>
<%@page import="com.zking.dao.NewsDao"%>
<%@page import="java.nio.charset.StandardCharsets"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link
href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css"
rel="stylesheet">
<script
src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script
src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body, html {
background: #7f8d90;
}
nav, .breadcrumb {
border-radius: 0px !important;
margin-bottom: 0px !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
li h4 {
width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<%@include file="top.jsp"%>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">首页</li>
</ol>
<%
//点击了表单之后 跳转的是当前这个页面 同时携带一个newName(查询的关键字)过来
String newName = request.getParameter("newName");
if (newName == null) {
newName = "";//查询所有
}
%>
<form action="${pageContext.request.contextPath}/news/index.jsp"
method="get" class="form-inline" style="margin: 0px auto 20px;">
<div class="form-group" style="display: block; text-align: center;">
<div class="input-group">
<div class="input-group-addon">新闻标题</div>
<input name="newName" value="<%=newName %>" class="form-control" placeholder="请在此输入搜索的关键字"
type="text"> <span class="input-group-btn">
<button class="btn btn-primary" type="submit">搜索🔍</button>
</span>
</div>
</div>
</form>
<div class="container">
<ul class="list-group">
<%
//设定当前用户进行分页的名字叫page
String index = request.getParameter("page");
int n = 1;
if (index != null) {
n = Integer.parseInt(index);
}
//破碎重组
//newName=new String(newName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
//查询当前数据的对应页数
int maxPage = new NewsDao().queryCount(newName);
//根据页数查询数据
for (News news : new NewsDao().queryByName(newName, n)) {
%>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a
href="${pageContext.request.contextPath}/news/read.jsp?newId=<%=news.getNewsId()%>"
data-placement="bottom" data-toggle="tooltip" href=""
title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例"> <%=news.getNewsTitle()%>
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code><%=news.getNewsAuthor()%></code></span>
<span class="glyphicon glyphicon-eye-open"><code><%=news.getNewsCount()%></code></span>
<span class="glyphicon glyphicon-tag"><code><%=news.getNewsMarker()%></code></span>
<span class="glyphicon glyphicon-time"><code><%=news.getNewsPublisher()%></code></span>
</p>
</li>
<%
}
%>
</ul>
</div>
<div class="container text-center">
<ul class="pagination" style="margin: 20px auto;">
<li><a href="index.jsp?page=<%=Math.max(n-1,1)%>&newName=<%=newName%>"><span>«</span></a></li>
<%
//根据最大页码 动态生成
for (int i = 1; i <= maxPage; i++) {
%>
<li class="<%=i==n?"active":""%>"><a href="index.jsp?page=<%=i%>&newName=<%=newName%>"><%=i %></a></li>
<%
}
%>
<li><a href="index.jsp?page=<%=Math.max(n+1,maxPage)%>&newName=<%=newName%>"><span>»</span></a></li>
</ul>
</div>
<script>
$(function() {
$('[data-toggle="tooltip"]').tooltip({
trigger : "hover"
})
})
</script>
</body>
</html>