欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaWeb入门教程之分页查询功能的简单实现

程序员文章站 2022-06-26 12:16:36
一、mysql实现分页查询的sql语句 1、分页需求:客户端通过传递pageno(页码),counter(每页显示的条数)两个参数去分页查询数据库表中的数据,那我们知道mysql数据库提供了分页的函数...

一、mysql实现分页查询的sql语句

1、分页需求:

客户端通过传递pageno(页码),counter(每页显示的条数)两个参数去分页查询数据库表中的数据,那我们知道mysql数据库提供了分页的函数limit m,n,但是该函数的用法和我们的需求不一样,所以就需要我们根据实际情况去改写适合我们自己的分页语句,具体的分析如下:

比如:

查询第1条到第10条的数据的sql是:select * from table limit 0,10;   ->对应我们的需求就是查询第一页的数据:select * from table limit (1-1)*10,10;

查询第10条到第20条的数据的sql是:select * from table limit 10,20;  ->对应我们的需求就是查询第二页的数据:select * from table limit (2-1)*10,10;

查询第20条到第30条的数据的sql是:select * from table limit 20,30;  ->对应我们的需求就是查询第三页的数据:select * from table limit (3-1)*10,10;

2、总结

通过上面的分析,可以得出符合我们自己需求的分页sql格式是:select * from table limit (pageno-1)*counter,counter; 其中pageno是页码,counter是每页显示的条数。

二、javaweb程序

1、创建pagebeanutils.java工具类

package com.ambow.utils;
import java.util.list;
public class pagebeanutils<t> {
 private int prepage;//上一页
 private int nextpage;//下一页
 private int firstpage=1;//首页
 private int lastpage;//尾页
 private int currentpage = 1;//当前
 private int totalpage;//总页数
 private int pagesize;//每页显示条数,默认显示10条
 private int totaldata;//数据总条数
 private list<t> pagedata;//数据
 public pagebeanutils(int currentpage,int pagesize, int totaldata) {
 this.currentpage = currentpage;
 this.pagesize = pagesize;
 this.totaldata = totaldata;
 
 //计算获得总页数(尾页)
// this.totalpage = this.lastpage = (totaldata+pagesize-1)/pagesize;
 this.totalpage = this.lastpage = (int)math.ceil((double)totaldata/pagesize);
 //防止当前页小于1
 this.currentpage = math.max(this.currentpage, 1);
 //防止当前页大于总的页数
 this.currentpage = math.min(this.totalpage, this.currentpage);
 //设置上一页,上一页不能小于1
 this.prepage = math.max(this.currentpage-1, 1);
 //设置下一页,下一页不能大于总页数
 this.nextpage = math.min(this.currentpage+1, this.totalpage);
 
 /**
 * ceil
public static double ceil(double a)
返回最小的(最接近负无穷大) double 值,该值大于等于参数,并等于某个整数。特殊情况如下:
如果参数值已经等于某个整数,那么结果与该参数相同。
如果参数为 nan、无穷大、正 0 或负 0,那么结果与参数相同。
如果参数值小于 0,但是大于 -1.0,那么结果为负 0。
注意, math.ceil(x) 的值与 -math.floor(-x) 的值完全相同。
参数:
a - 一个值。
返回:
最小(最接近负无穷大)浮点值,该值大于等于该参数,并等于某个整数。
 */
 
 }
 
 public pagebeanutils(int prepage, int nextpage, int firstpage, int lastpage, int currentpage, int totalpage,
 int pagesize, int totaldata, list<t> pagedata) {
 super();
 this.prepage = prepage;
 this.nextpage = nextpage;
 this.firstpage = firstpage;
 this.lastpage = lastpage;
 this.currentpage = currentpage;
 this.totalpage = totalpage;
 this.pagesize = pagesize;
 this.totaldata = totaldata;
 this.pagedata = pagedata;
 }
 public int getprepage() {
 return prepage;
 }
 public void setprepage(int prepage) {
 this.prepage = prepage;
 }
 public int getnextpage() {
 return nextpage;
 }
 public void setnextpage(int nextpage) {
 this.nextpage = nextpage;
 }
 public int getfirstpage() {
 return firstpage;
 }
 public void setfirstpage(int firstpage) {
 this.firstpage = firstpage;
 }
 public int getlastpage() {
 return lastpage;
 }
 public void setlastpage(int lastpage) {
 this.lastpage = lastpage;
 }
 public int getcurrentpage() {
 return currentpage;
 }
 public void setcurrentpage(int currentpage) {
 this.currentpage = currentpage;
 }
 public int gettotalpage() {
 return totalpage;
 }
 public void settotalpage(int totalpage) {
 this.totalpage = totalpage;
 }
 public int getpagesize() {
 return pagesize;
 }
 public void setpagesize(int pagesize) {
 this.pagesize = pagesize;
 }
 public int gettotaldata() {
 return totaldata;
 }
 public void settotaldata(int totaldata) {
 this.totaldata = totaldata;
 }
 public list<t> getpagedata() {
 return pagedata;
 }
 public void setpagedata(list<t> pagedata) {
 this.pagedata = pagedata;
 }
 
 
 /*
 * 
 * 
 * totalpage = (totaldata+pagesize-1)/pagesize;
 * 
 * 
 * */
}

2、在接口里面定义分页查询的方法

package com.ambow.dao;
 
import java.util.list;
 
import com.ambow.pojo.good;
 
public interface igooddao {
 //增删改查
 public void add(good good);
 public void delete(good good);
 public void update(good good);
 public void query(good good);
 public good queryone(good good);
 public list<good> querymore(good good);
 public list<good> querybyname(string name);//根据商家名称进行模糊查询
 //条件分页查询
 public list<good> querybyname(string name,int currentpage,int pagesize);
 //获取满足某个条件的总记录数
 public int gettotalnum(string name);
}

3、在接口的实现类里面实现方法

package com.ambow.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 com.ambow.dao.igooddao;
import com.ambow.pojo.good;
import com.ambow.utils.dbutils;
 
public class gooddaoimpl implements igooddao {
 dbutils db = new dbutils();
 @override
 public void add(good good) {
 connection conn = null;
 preparedstatement pstmt = null;
 string sql = "insert into good (name,address,tel,dishes) values (?,?,?,?)";
 try {
 conn = dbutils.getconnection();
 pstmt = conn.preparestatement(sql);
 pstmt.setstring(1, good.getname());
 pstmt.setstring(2, good.getaddress());
 pstmt.setstring(3, good.gettel());
 pstmt.setstring(4, good.getdishes());
 int isok = pstmt.executeupdate();
 //system.out.println("add-----"+isok);
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 }
 
 @override
 public void delete(good good) {
 connection conn = null;
 preparedstatement pstmt = null;
 string sql = "delete from good where id = ?";
 try {
 conn = dbutils.getconnection();
 pstmt = conn.preparestatement(sql);
 pstmt.setint(1, good.getid());
 int isok = pstmt.executeupdate();
 system.out.println("delete-----"+isok);
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 
 }
 
 @override
 public void update(good good) {
 connection conn = null;
 preparedstatement pstmt = null;
 string sql = "update good set name=?,address=?,tel=?,dishes=? where id=?";
 try {
 conn = dbutils.getconnection();
 pstmt = conn.preparestatement(sql);
 pstmt.setstring(1, good.getname());
 pstmt.setstring(2, good.getaddress());
 pstmt.setstring(3, good.gettel());
 pstmt.setstring(4, good.getdishes());
 pstmt.setint(5,good.getid());
 int isok = pstmt.executeupdate();
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 
 }
 
 @override
 public void query(good good) {
 // todo auto-generated method stub
 
 }
 
 @override
 public good queryone(good good) {
 connection conn = null;
 preparedstatement pstmt = null;
 resultset rs = null;
 string sql = "select * from good where id = ?";
 good gd = null;
 try {
 conn = dbutils.getconnection();
 pstmt = conn.preparestatement(sql);
 pstmt.setint(1, good.getid());
 rs = pstmt.executequery();
 while(rs.next()){
  gd = new good(rs.getint(1),rs.getstring(2),rs.getstring(3),
  rs.getstring(4),rs.getstring(5));
 }
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return gd;
 
 }
 
 @override
 public list<good> querymore(good good) {
 // todo auto-generated method stub
 return null;
 }
 
 @override
 public list<good> querybyname(string name) {
 connection conn = null;
 preparedstatement pstmt = null;
 resultset rs = null;
 string sql = "select * from good where name like ?";
 list<good> goodlist = new arraylist<good>();
 try {
 conn = dbutils.getconnection();
 pstmt = conn.preparestatement(sql);
 pstmt.setstring(1, "%"+name+"%");
 rs = pstmt.executequery();
 while(rs.next()){
  goodlist.add(new good(rs.getint(1),rs.getstring(2),rs.getstring(3),
  rs.getstring(4),rs.getstring(5)));
 }
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return goodlist;
 }
 
 @override
 public list<good> querybyname(string name, int currentpage, int pagesize) {
 connection conn = null;
 preparedstatement pstmt = null;
 resultset rs = null;
 string sql = "select * from good where name like ? limit ?,?";
 list<good> goodlist = new arraylist<good>();
 try {
 conn = dbutils.getconnection();
 pstmt = conn.preparestatement(sql);
 pstmt.setstring(1, "%"+name+"%");
 pstmt.setint(2,(currentpage-1)*pagesize);
 pstmt.setint(3,pagesize);
 rs = pstmt.executequery();
 while(rs.next()){
  goodlist.add(new good(rs.getint(1),rs.getstring(2),rs.getstring(3),
  rs.getstring(4),rs.getstring(5)));
 }
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return goodlist;
 
 }
 
 @override
 public int gettotalnum(string name) {
 connection conn = null;
 preparedstatement pstmt = null;
 resultset rs = null;
 string sql = "select count(id) from good where name like ?";
 int total = 0;
 try {
 conn = dbutils.getconnection();
 pstmt = conn.preparestatement(sql);
 pstmt.setstring(1, "%"+name+"%");
 rs = pstmt.executequery();
 while(rs.next()){
  total = rs.getint(1);
 }
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return total;
 
 }
 
}

4.在servlet里面调用实现类里面的分页查询方法

package com.ambow.servlet;
 
import java.io.ioexception;
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 com.ambow.dao.igooddao;
import com.ambow.dao.impl.gooddaoimpl;
import com.ambow.pojo.good;
import com.ambow.utils.pagebeanutils;
 
@webservlet("/queryservlet")
public class queryservlet extends httpservlet {
 private static final long serialversionuid = 1l;
 private igooddao gooddao = new gooddaoimpl();
 public queryservlet() {
  super();
 }
 
 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
 dopost(request,response);
 }
 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
 request.setcharacterencoding("utf-8");
 response.setcharacterencoding("utf-8");
 string keywords = request.getparameter("kw");
 string method = request.getparameter("method");
 if("add".equals(method)){
 string name = request.getparameter("name");
 string address = request.getparameter("address");
 string tel = request.getparameter("tel");
 string dishes = request.getparameter("dishes");
 good good = new good(0,name,address,tel,dishes);
 gooddao.add(good);
 //调用dao的查询方法,返回一个list
 list<good> goods = gooddao.querybyname(keywords);
 request.setattribute("list", goods);
 request.getrequestdispatcher("goods.jsp").forward(request, response);
 }else if("search".equals(method)){
 if(null==keywords) {
 keywords="";
 }
 int currentpage = 1;
 try {
 currentpage=integer.parseint(request.getparameter("curpage"));
 if(currentpage<=0) {
  currentpage = 1;
 }
 }catch(exception e) {
 currentpage = 1;
 }
 int pagesize=10;
 int totaldata = gooddao.gettotalnum(keywords);
 int totalpage = (int)math.ceil((double)totaldata/pagesize);
 if(currentpage>totalpage){
 currentpage = totalpage;
 }
 list<good> goods = gooddao.querybyname(keywords,currentpage,pagesize); 
 pagebeanutils pg = new pagebeanutils(currentpage,pagesize,totaldata);
 pg.setpagedata(goods);
 request.setattribute("pg", pg);
 request.getrequestdispatcher("good2.jsp").forward(request, response);
 }else if("delete".equals(method)){
 system.out.println(keywords);
 //实现删除
 string id = request.getparameter("id");
 good good = new good();
 good.setid(integer.valueof(id));
 gooddao.delete(good);
 //调用dao的查询方法,返回一个list
 list<good> goods = gooddao.querybyname(keywords);
 request.setattribute("list", goods);
 request.getrequestdispatcher("goods.jsp").forward(request, response);
 }else if("querybyid".equals(method)){
 //查询一个
 string id = request.getparameter("id");
 good good = new good();
 good.setid(integer.valueof(id));
 good = gooddao.queryone(good);
 //调用dao的查询方法,返回一个good
 request.setattribute("good", good);
 request.getrequestdispatcher("update.jsp").forward(request, response);
 }else if("update".equals(method)){
 string id = request.getparameter("id");
 string name = request.getparameter("name");
 string address = request.getparameter("address");
 string tel = request.getparameter("tel");
 string dishes = request.getparameter("dishes");
 good good = new good(integer.valueof(id),name,address,tel,dishes);
 gooddao.update(good);
 //调用dao的查询方法,返回一个list
 list<good> goods = gooddao.querybyname(keywords);
 request.setattribute("list", goods);
 request.getrequestdispatcher("goods.jsp").forward(request, response);
 }else{
 //调用dao的查询方法,返回一个list
 list<good> goods = gooddao.querybyname(keywords);
 request.setattribute("list", goods);
 request.getrequestdispatcher("goods.jsp").forward(request, response);
 }
 }
 
}

5.在jsp页面获取servlet里面传过来的数据

<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8" import="java.util.arraylist,com.ambow.pojo.good,
 com.ambow.pojo.user,com.ambow.utils.pagebeanutils"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>外卖系统的搜索功能</title>
</head>
<body>
 <form action="queryservlet?method=search" method="post">
 <input type="text" name="kw"/><input type="submit" value="搜索"/><a href=" ">添加商家</a >
 <table border="1">
 <tr><th>商家店名</th><th>商家地址</th><th>商家电话</th><th>经营菜品</th><th colspan="2">编辑</th></tr>
 <c:foreach items="${pg.pagedata}" var="g">
  <tr><td>${g.name}</td><td>${g.address}</td><td>${g.tel}</td><td>${g.dishes}</td>
  <td><a href="queryservlet?method=querybyid&id=${g.id}">修改</a ></td>
  <td><a href="queryservlet?method=delete&id=${g.id}" onclick="return confirm('确认删除本条数据吗?');">删除</a ></td></tr>
 
 </c:foreach>
 </table>
 <a href="queryservlet?method=search&curpage=${pg.firstpage}">首页</a >
 <a href="queryservlet?method=search&curpage=${pg.currentpage - 1}">上一页</a >
 <a href="queryservlet?method=search&curpage=${pg.currentpage + 1}">下一页</a >
 <a href="queryservlet?method=search&curpage=${pg.lastpage}">尾页</a >
 当前第${pg.currentpage}页/共${pg.totalpage}页
 每页显示${pg.pagesize}条
 </form>
 
</body>
</html>

总结

到此这篇关于javaweb入门教程之分页查询功能的文章就介绍到这了,更多相关javaweb分页查询功能内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!