java web實(shí)現(xiàn)分頁(yè)查詢實(shí)例方法
Javaweb分頁(yè)技術(shù)實(shí)現(xiàn)
分頁(yè)技術(shù)就是通過SQL語(yǔ)句(如下)來獲取數(shù)據(jù),具體實(shí)現(xiàn)看下面代碼
//分頁(yè)查詢語(yǔ)句 select * from 表名 where limit page , count; 和 //獲取表中的總數(shù)據(jù),確定頁(yè)數(shù) select count(*) from 表名;

不說廢話直接上代碼
前端代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css" rel="external nofollow" >
</head>
<body>
<div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>City_ID</th>
<th>City_EN</th>
<th>City_CN</th>
<th>Country_code</th>
<th>Country_EN</th>
<th>Country_CN</th>
<th>Province_EN</th>
<th>Province_CN</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<!--分頁(yè)-->
第<span id="paging">1</span>頁(yè)/
共<span id="countPage">1</span>頁(yè)/
<a id="homePage">Home</a>/
<a id="prevPage">Prev</a>/
<a id="nextPage">Next</a>/
轉(zhuǎn)到第:
<input type="text" style="width: 2em" id="pageNum">
頁(yè)
<a id="goPage">Go</a>
</div>
</div>
</div>
</body>
<script>
$(function () {
//頁(yè)面初始化 (顯示第一頁(yè))
selectPage(1);
home();
prev();
next();
goPage();
})
function selectPage(pageCode) {
//分頁(yè)查詢 pageCode:頁(yè)數(shù)
$.ajax("getCity",{
type:"get",
data:{"currenPage":pageCode},
success:function (data) {
$("#tbody").html("");
//總頁(yè)數(shù)
$("#countPage").text(data.totalPage);
$.each(data.pageData,function (index,obj) {
var clazz="";
if(index%2==0){
clazz="success";
}
$("#tbody").append(
"<tr class='"+clazz+"'>\n" +
"<td>"+obj.cityId+"</td>\n" +
"<td>"+obj.cityEn+"</td>\n" +
"<td>"+obj.cityCn+"</td>\n" +
"<td>"+obj.countryCode+"</td>\n" +
"<td>"+obj.countryEn+"</td>\n" +
"<td>"+obj.countryCn+"</td>\n" +
"<td>"+obj.provinceEn+"</td>\n" +
"<td>"+obj.provinceCn+"</td>\n" +
"</tr>"
);
})
}
});
}
//第一頁(yè)
function home() {
$("#homePage").on("click",function () {
$("#paging").text(1);
selectPage(1);
})
}
//上一頁(yè)
function prev() {
$("#prevPage").on("click",function () {
var prevText=$("#paging").text();
var prevNum=parseInt(prevText);
prevNum=prevNum-1;
if(prevNum<=1){
selectPage(1);
$("#paging").text(1);
return;
}
$("#paging").text(prevNum);
selectPage(prevNum);
})
}
//下一頁(yè)
function next() {
$("#nextPage").on("click",function () {
//獲取文本的值 頁(yè)數(shù)
var prevText=$("#paging").text();
//類型轉(zhuǎn)換
var prevNum=parseInt(prevText);
//總頁(yè)數(shù)
var countText=$("#countPage").text();
//類型轉(zhuǎn)換
var countNum = parseInt(countText);
//頁(yè)數(shù)加1
prevNum=prevNum+1;
//判斷超出了總頁(yè)碼
if(prevNum>=countNum){
selectPage(countNum);
$("#paging").text(countNum);
return;
}
//設(shè)置網(wǎng)頁(yè)增加的值
$("#paging").text(prevNum);
//調(diào)用分頁(yè)查詢
selectPage(prevNum);
})
}
//去到幾頁(yè)
function goPage() {
$("#goPage").on("click",function () {
var pageNum=parseInt($("#pageNum").val());
var countPage=parseInt($("#countPage").text())
//判斷超出了總頁(yè)碼
if(pageNum>=countPage){
selectPage(countPage);
$("#paging").text(countPage);
$("#pageNum").val(countPage);
return;
}
//判斷低于了總頁(yè)碼
if(pageNum<=1){
selectPage(1);
$("#paging").text(1);
$("#pageNum").val(1);
return;
}
selectPage(pageNum);
$("#paging").text(pageNum);
})
}
</script>
</html>
后臺(tái)servlet代碼:
/**
* @author hh
* @Date 2018/9/12
*/
@WebServlet("/getCity")
public class PageServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取當(dāng)前頁(yè)參數(shù),第一次訪問為空
String currPage = req.getParameter("currenPage");
// 判斷,如果為空,則設(shè)置為1
if (currPage == null || "".equals(currPage.trim())) {
currPage = "1";
}
//調(diào)用service返回分頁(yè)類實(shí)例
PageBean<City> pageBean=new PageService().getPage(currPage);
//設(shè)置相應(yīng)文本類型
resp.setContentType("application/json;charset=utf-8");
//響應(yīng)前端
resp.getWriter().print(new Gson().toJson(pageBean));
}
}
City 實(shí)體類:
package edu.nf.demo.entity;
/**
* @author hh
* @Date 2018/9/12
*/
public class City {
private String cityId;
private String cityEn;
private String cityCn;
private String countryCode;
private String countryEn;
private String countryCn;
private String provinceEn;
private String provinceCn;
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCityEn() {
return cityEn;
}
public void setCityEn(String cityEn) {
this.cityEn = cityEn;
}
public String getCityCn() {
return cityCn;
}
public void setCityCn(String cityCn) {
this.cityCn = cityCn;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryEn() {
return countryEn;
}
public void setCountryEn(String countryEn) {
this.countryEn = countryEn;
}
public String getCountryCn() {
return countryCn;
}
public void setCountryCn(String countryCn) {
this.countryCn = countryCn;
}
public String getProvinceEn() {
return provinceEn;
}
public void setProvinceEn(String provinceEn) {
this.provinceEn = provinceEn;
}
public String getProvinceCn() {
return provinceCn;
}
public void setProvinceCn(String provinceCn) {
this.provinceCn = provinceCn;
}
}
自己寫的一個(gè)類,專門用于分頁(yè)查詢用的:
package edu.nf.demo.entity;
import java.util.List;
/**
* @author hh
* @Date 2018/9/12
*/
public class PageBean<T> {
/**
* 當(dāng)前頁(yè), 默認(rèn)顯示第一頁(yè)
*/
private Integer currntPage = 1;
/**
* 查詢返回的行數(shù)(每頁(yè)顯示的行數(shù)),默認(rèn)每頁(yè)顯示10行
*/
private int pageCount = 10;
/**
* 總記錄數(shù)
*/
private int totalCount;
/**
* 總頁(yè)數(shù) = 總記錄數(shù)/每頁(yè)顯示的行數(shù)(+1)
*/
private int totalPage;
/**
* 分頁(yè)查詢的數(shù)據(jù),運(yùn)用泛型,可以重復(fù)利用
*/
private List<T> pageData;
public int getTotalPage() {
if (totalCount % pageCount == 0) {
totalPage = totalCount / pageCount;
} else {
totalPage = totalCount / pageCount + 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrntPage() {
return currntPage;
}
public void setCurrntPage(int currntPage) {
this.currntPage = currntPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List<T> getPageData() {
return pageData;
}
public void setPageData(List<T> pageData) {
this.pageData = pageData;
}
}
后臺(tái)service,邏輯業(yè)務(wù)層:
/**
* @author hh
* @Date 2018/9/12
*/
public class PageService {
public PageBean getPage(String currPage){
//類型轉(zhuǎn)換 當(dāng)前頁(yè)數(shù)
Integer currenPage = Integer.valueOf(currPage);
//實(shí)例化分頁(yè)類
PageBean<City> pageBean = new PageBean();
//實(shí)例化CityDaoImpl類
CityDaoImpl cityDao=new CityDaoImpl();
//數(shù)據(jù)庫(kù)第幾行開始查詢
int startPage=(currenPage-1)*pageBean.getPageCount();
//查詢多少行數(shù)據(jù) 分頁(yè)類里默認(rèn)30行
int selectCount=pageBean.getPageCount();
//查詢數(shù)據(jù)庫(kù)獲取分頁(yè)返回的數(shù)據(jù) : select * from regional_info limit startPage,selectCount
List<City> list=cityDao.listCity(startPage,selectCount);
//獲取總數(shù)
int cityCount=cityDao.getCityCount();
//設(shè)置查詢的數(shù)據(jù)
pageBean.setPageData(list);
//共多少行
pageBean.setTotalCount(cityCount);
//設(shè)置總頁(yè)數(shù)
pageBean.setTotalPage(cityCount/pageBean.getPageCount()+1);
return pageBean;
}
}
相關(guān)文章
java中hashCode方法與equals方法的用法總結(jié)
總的來說,Java中的集合(Collection)有兩類,一類是List,再有一類是Set。前者集合內(nèi)的元素是有序的,元素可以重復(fù);后者元素?zé)o序,但元素不可重復(fù)2013-10-10
Spring AOP與AspectJ的對(duì)比及應(yīng)用詳解
這篇文章主要為大家介紹了Spring AOP與AspectJ的對(duì)比及應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié)
這篇文章主要介紹了SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決
這篇文章主要介紹了SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
淺談HttpClient、okhttp和RestTemplate的區(qū)別
這篇文章主要介紹了HttpClient、okhttp和RestTemplate的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
spring-session簡(jiǎn)介及實(shí)現(xiàn)原理源碼分析
這篇文章主要介紹了spring-session簡(jiǎn)介及實(shí)現(xiàn)原理源碼分析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

