public class BaseBean { private Integer page = 1 ; public Integer getPage () { return page; } }
import java.util.List;public class QueryResult <T> { private List<T> list ; private long totalRecord; public List<T> getList() { return list ; } public void setList (List<T> list ) { this .list = list ; } public long getTotalRecord () { return totalRecord; } public void setTotalRecord (Long totalRecord) { this .totalRecord = totalRecord; } }public class BaseBean { private Integer page = 1 ; public Integer getPage () { return page; } }
public class User extends BaseBean { private Integer id; private String userName; private Integer age; public User (Integer id,String userName, Integer age) { super (); this .id=id; this .userName = userName; this .age = age; } public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getUserName () { return userName; } public void setUserName (String userName) { this .userName = userName; } public Integer getAge () { return age; } public void setAge (Integer age) { this .age = age; } }
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import com.xukaiqiang.bean.QueryResult;import com.xukaiqiang.bean.User;import com.xukaiqiang.util.DBHelper;public class UserDao { */ @Description :分页查询数据库中的数据 *firstIndex分页的首个参数,表示从第几条记录开始索引 *pageNum分页的第二个参数,表示索引几条 */ public QueryResult<User> queryUser (int firstIndex, int pageNum) { QueryResult<User> users = new QueryResult<>(); List<User> users2 = new ArrayList<>(); Connection connection = null ; try { connection = DBHelper.getConn(); StringBuffer sql = new StringBuffer("select * from t_user" ); sql.append(" limit " + firstIndex + "," + pageNum); PreparedStatement preparedStatement = connection.prepareStatement(sql.toString()); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { User user = new User(resultSet.getInt("id" ),resultSet.getString("userName" ), resultSet.getInt("age" )); users2.add(user); } users.setList(users2); users.setTotalRecord(queryCount()); } catch (Exception e) { e.printStackTrace(); } finally { DBHelper.closeConn(connection); } return users; } public long queryCount () { long totalCount = 0 ; Connection connection = null ; try { connection = DBHelper.getConn(); StringBuffer sql = new StringBuffer("select count(*) num from t_user" ); PreparedStatement preparedStatement = connection.prepareStatement(sql.toString()); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { totalCount = resultSet.getLong("num" ); } } catch (Exception e) { e.printStackTrace(); } return totalCount; } }
import java.util.List;import com.xukaiqiang.util.Globals;public class PageView <T> { private long totalPage; private Integer currentPage; private long pageNum = Globals.PAGE_NUM; private long totalRecord; private List<T> list ; private String pageHtml; public PageView (Integer currentPage, long totalRecord, List<T> list ) { super(); this .currentPage = currentPage; this .totalRecord = totalRecord; this .list = list ; setTotalPage(this .totalRecord % this .pageNum == 0 ? this .totalRecord / this .pageNum : this .totalRecord / this .pageNum + 1 ); StringBuffer pageHtml = new StringBuffer(); pageHtml.append("共有" + this .totalRecord + "条记录/第" + this .currentPage + "页/共" + this .totalPage + "页" ); if (this .currentPage > 1 ) { pageHtml.append("<a href='javascript:query(1)'>首页</a>" ); } if (this .currentPage > 1 ) { pageHtml.append("<a href='javascript:query(" + (this .currentPage - 1 ) + ")'>上一页</a>" ); } if (this .currentPage < this .totalPage) { pageHtml.append("<a href='javascript:query(" + (this .currentPage + 1 ) + ")'>下一页</a>" ); pageHtml.append("<a href='javascript:query(" + this .totalPage + ")'>末页</a>" ); } setPageHtml(pageHtml.toString()); } public String getPageHtml () { return pageHtml; } public void setPageHtml (String pageHtml) { this .pageHtml = pageHtml; } public List<T> getList() { return list ; } public void setList (List<T> list ) { this .list = list ; } public long getTotalPage () { return totalPage; } public void setTotalPage (long totalPage) { this .totalPage = totalPage; } public Integer getCurrentPage () { return currentPage; } public void setCurrentPage (Integer currentPage) { this .currentPage = currentPage; } public long getPageNum () { return pageNum; } public void setPageNum (long pageNum) { this .pageNum = pageNum; } public long getTotalRecord () { return totalRecord; } public void setTotalRecord (long totalRecord) { this .totalRecord = totalRecord; } }
import java.util.List;import com.xukaiqiang.util.Globals;public class PageViewBaidu <T > { private int totalPage; private int nowPage; private int startPage; private int endPage; private int totalRecord; private String pageHtml2; private List<T> list; public static final int PAGESIZE = Globals.PAGE_NUM; public static final int SHOWPAGES = Globals.SHOWPAGES; public PageViewBaidu(Integer nowPage, int totalRecord, List<T> list) { super (); this .nowPage = nowPage; this .totalRecord = totalRecord; this .list = list; setTotalPage(this .totalRecord % PAGESIZE == 0 ? this .totalRecord / PAGESIZE : this .totalRecord / PAGESIZE + 1 ); if (this .totalPage < SHOWPAGES) { this .startPage = 1 ; this .endPage = totalPage; } else { if (this .nowPage <= SHOWPAGES / 2 + 1 ) { this .startPage = 1 ; this .endPage = SHOWPAGES; } else { this .startPage = this .nowPage - (SHOWPAGES / 2 ); this .endPage = this .nowPage + (SHOWPAGES / 2 - 1 ); if (this .endPage >= this .totalPage) { this .endPage = this .totalPage; this .startPage = this .totalPage - SHOWPAGES + 1 ; } } } StringBuffer pageHtml = new StringBuffer(); pageHtml.append("<nav><ul class='pagination pagination-lg'>" ); if (this .nowPage > 1 ) { pageHtml.append("<li><a href='javascript:query(" + (this .nowPage - 1 ) + ")' aria-label='上一页'><span aria-hidden='true'><上一页</span></a>" ); } for (int i = this .startPage; i <= this .endPage; i++) { if (i != this .nowPage) { pageHtml.append("<li><a href='javascript:query(" + i + ")'>" + i + " </a></li>" ); }else { pageHtml.append("<li class='active'><a href='javascript:query(" + i + ")'>" + i + " </a></li>" ); } } if (this .nowPage != this .totalPage) { pageHtml.append("<li><a href='javascript:query(" + (this .nowPage + 1 ) + ")' aria-label='下一页'><span aria-hidden='true'>下一页></span></a></li>" ); } pageHtml.append("</ul></nav>" ); setPageHtml2(pageHtml.toString()); } public void paginationTool(int nowPage, int totalPage) { this .nowPage = nowPage; this .totalPage = totalPage; if (this .totalPage < SHOWPAGES) { this .startPage = 1 ; this .endPage = totalPage; } else { if (this .nowPage <= SHOWPAGES / 2 + 1 ) { this .startPage = 1 ; this .endPage = SHOWPAGES; } else { this .startPage = this .nowPage - (SHOWPAGES / 2 ); this .endPage = this .nowPage + (SHOWPAGES / 2 - 1 ); if (this .endPage >= this .totalPage) { this .endPage = this .totalPage; this .startPage = this .totalPage - SHOWPAGES + 1 ; } } } } public int getNowPage() { return nowPage; } public void setNowPage(int nowPage) { this .nowPage = nowPage; } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this .totalRecord = totalRecord; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this .totalPage = totalPage; } public String getPageHtml2() { return pageHtml2; } public void setPageHtml2(String pageHtml2) { this .pageHtml2 = pageHtml2; } public List<T> getList() { return list; } public void setList(List<T> list) { this .list = list; } public int getStartPage() { return startPage; } public void setStartPage(int startPage) { this .startPage = startPage; } public int getEndPage() { return endPage; } public void setEndPage(int endPage) { this .endPage = endPage; } }
import java.io.IOException;import java.lang.reflect.Type;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.google.gson.reflect.TypeToken;import com.xukaiqiang.bean.QueryResult;import com.xukaiqiang.bean.User;import com.xukaiqiang.dao.UserDao;import com.xukaiqiang.page.PageViewBaidu;import com.xukaiqiang.util.Globals;import com.xukaiqiang.util.Utils;@WebServlet ("/UserServlet" )public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L ; protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int currentPage = Integer.parseInt(request.getParameter("page" )); UserDao userDao = new UserDao(); QueryResult<User> users = userDao.queryUser((currentPage - 1 ) * Globals.PAGE_NUM, Globals.PAGE_NUM); PageViewBaidu<User> pageView=new PageViewBaidu<>(currentPage, (int ) users.getTotalRecord(), users.getList()); Type listType = new TypeToken<PageViewBaidu<User>>() { }.getType(); } }
import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBHelper { public static Connection getConn () { Connection connection = null ; try { Class.forName("com.mysql.jdbc.Driver" ); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test" , "root" , "root" ); } catch (Exception e) { e.printStackTrace(); } return connection; } public static void closeConn (Connection connection) { try { if (connection != null ) { connection.close(); } }catch (Exception e) { e.printStackTrace(); } } }
public class Globals { public static final int PAGE_NUM = 5 ; public static final int SHOWPAGES = 10 ; }
import java.io.IOException;import java.io.PrintWriter;import java.lang.reflect.Type;import javax.servlet.http.HttpServletResponse;import com.google.gson.Gson;public class Utils { public static String printInfo(Object object , HttpServletResponse response,Type listType) { Gson gson = new Gson(); String result = gson.toJson(object ,listType); response.setContentType("text/json; charset=utf-8" ); response.setHeader("Cache-Control" , "no-cache" ); PrintWriter out = null ; try { out = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } out .print(result); out .flush(); out .close(); return null ; } }
<%@ 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 > 分页</title > <link rel ="stylesheet" href ="bootstrap/css/bootstrap.min.css" > <script type ="text/javascript" src ="js/jquery-1.10.2.js" > </script > <script src ="bootstrap/js/bootstrap.min.js" > </script > <script type ="text/javascript" > function query (page ) { var userName = $("#userName" ).val(); var dataUrl = "UserServlet" ; var params = { "userName" : userName, "page" : page }; $ .ajax({ async : true , url : dataUrl, data : params, dataType : "json" , cache : false , type : "POST" , error : function (textStatus, errorThrown ) { alert("ajax请求失败!" ); }, beforeSend : function ( ) { }, success : function (data, textStatus ) { var innerHtml = "<table class='table table-bordered' style='width:50%'>" ; innerHtml += "<th>编号<th>姓名</th><th>年龄</th>" ; $.each(data.list, function (index, user ) { innerHtml += "<tr>" ; innerHtml += "<td>" + user.id + "</td>" ; innerHtml += "<td>" + user.userName + "</td>" ; innerHtml += "<td>" + user.age + "</td>" ; innerHtml += "</tr>" ; }); innerHtml += "</table>" ; $("#dataList" ).html("" ); $("#dataList" ).html(innerHtml); $("#pageHTML2" ).html(data.pageHtml2); } }); }</script > </head > <body onload ="query(1)" > <center > <h2 > 百度分页</h2 > <hr > <div id ="dataList" > </div > <div id ="pageHTML" > </div > <div id ="pageHTML2" > </div > </center > </body > <script > var a_idx = 0 ; $("body" ).click(function (e ) { var a = new Array ( "富强" , "民主" , "文明" , "和谐" , "自由" , "平等" , "公正" , "法治" , "爱国" , "敬业" , "诚信" , "友善" ); var $i = $("<span/>" ).text(a[a_idx]); a_idx = (a_idx + 1 ) % a.length; var x = e.pageX, y = e.pageY; $i.css({ "z-index" : 144469 , "top" : y - 20 , "left" : x, "position" : "absolute" , "font-weight" : "bold" , "color" : "#f00" }); $("body" ).append($i); $i.animate({ "top" : y - 180 , "opacity" : 0 }, 1500 , function ( ) { $i.remove() }) });</script > </html >