Java實(shí)現(xiàn)分頁(yè)代碼
在項(xiàng)目中,分頁(yè)是一個(gè)項(xiàng)目中必不可少的,它可以防止我們從數(shù)據(jù)庫(kù)中進(jìn)行大量數(shù)據(jù)查詢時(shí)速度變慢,提高我們的查詢效率。
1、定義分頁(yè)模型:PageModel
package com.common.page;
import java.util.List;
/**
* 封裝分頁(yè)信息
* @author Administrator
*
*/
public class PageModel<E> {
//結(jié)果集
private List<E> list;
//查詢記錄數(shù)
private int totalRecords;
//每頁(yè)多少條數(shù)據(jù)
private int pageSize;
//第幾頁(yè)
private int pageNo;
/**
* 總頁(yè)數(shù)
* @return
*/
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 取得首頁(yè)
* @return
*/
public int getTopPageNo() {
return 1;
}
/**
* 上一頁(yè)
* @return
*/
public int getPreviousPageNo() {
if (pageNo <= 1) {
return 1;
}
return pageNo - 1;
}
/**
* 下一頁(yè)
* @return
*/
public int getNextPageNo() {
if (pageNo >= getBottomPageNo()) {
return getBottomPageNo();
}
return pageNo + 1;
}
/**
* 取得尾頁(yè)
* @return
*/
public int getBottomPageNo() {
return getTotalPages();
}
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
2、分頁(yè)測(cè)試:在MySQL中建立admin表,里面有字段id、name、password
3、簡(jiǎn)歷Admin的實(shí)體bean類:
package com.common.page;
public class Admin {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4、測(cè)試調(diào)用:
package com.common.page;
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.common.db.DbUtil;
public class Client {
public static PageModel findAdmins(int pageNo,int pageSize){
Connection conn=DbUtil.getConnection();
String sql="select * from admin limit ?,?";
PageModel pageModel=null;
PreparedStatement pstm=null;
ResultSet rs=null;
Admin admin=null;
List<Admin> list=new ArrayList<Admin>();
try {
pstm=conn.prepareStatement(sql);
pstm.setInt(1, (pageNo-1)*pageSize);
pstm.setInt(2, pageNo*pageSize);
rs=pstm.executeQuery();;
while(rs.next()){
admin=new Admin();
admin.setId(rs.getInt("a_id"));
admin.setName(rs.getString("a_name"));
admin.setPassword(rs.getString("a_pwd"));
list.add(admin);
}
ResultSet rs2=pstm.executeQuery("select count(*) from admin");
int total=0;
if(rs2.next()){
total=rs2.getInt(1);
}
pageModel=new PageModel();
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setTotalRecords(total);
pageModel.setList(list);
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.close(conn);
DbUtil.close(pstm);
DbUtil.close(rs);
}
return pageModel;
}
public static void main(String[] args) {
PageModel pageModel=Client.findAdmins(2,4);
List<Admin> list=pageModel.getList();
for(Admin a:list){
System.out.print("ID:"+a.getId()+",用戶名:"+a.getName()+",密碼:"+a.getPassword());
System.out.println();
}
System.out.print("當(dāng)前頁(yè):"+pageModel.getPageNo()+" ");
System.out.print("共"+pageModel.getTotalPages()+"頁(yè) ");
System.out.print("首頁(yè):"+pageModel.getTopPageNo()+" ");
System.out.print("上一頁(yè):"+pageModel.getPreviousPageNo()+" ");
System.out.print("下一頁(yè):"+pageModel.getNextPageNo()+" ");
System.out.print("尾頁(yè):"+pageModel.getBottomPageNo()+" ");
System.out.print("共"+pageModel.getTotalRecords()+"條記錄");
System.out.println();
}
}
這樣分頁(yè)效果就實(shí)現(xiàn)了,我們要實(shí)現(xiàn)分頁(yè)效果,只要傳入相應(yīng)的參數(shù)和相應(yīng)的數(shù)據(jù)庫(kù)執(zhí)行語(yǔ)句即可實(shí)現(xiàn),希望大家能靈活運(yùn)用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用SpringBoot讀取Windows共享文件的代碼示例
在現(xiàn)代企業(yè)環(huán)境中,文件共享是一個(gè)常見(jiàn)的需求,Windows共享文件夾(SMB/CIFS協(xié)議)因其易用性和廣泛的兼容性,成為了許多企業(yè)的首選,在Java應(yīng)用中,尤其是使用Spring Boot框架時(shí),如何讀取Windows共享文件是一個(gè)值得探討的話題,本文介紹了使用SpringBoot讀取Windows共享文件2024-11-11
springboot3 使用 jasypt 加密配置文件的使用步驟
在SpringBoot項(xiàng)目中,使用Jasypt加密配置文件可以有效保護(hù)敏感信息,首先,需添加Jasypt依賴并配置加密密碼,可在application.properties或通過(guò)啟動(dòng)參數(shù)、環(huán)境變量設(shè)置,本文介紹了Jasypt的配置步驟及使用方法,幫助開(kāi)發(fā)者保護(hù)應(yīng)用配置信息2024-11-11
java(包括springboot)讀取resources下文件方式實(shí)現(xiàn)
這篇文章主要介紹了java(包括springboot)讀取resources下文件方式實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
MyBatis簡(jiǎn)介與配置MyBatis+Spring+MySql的方法
MyBatis 是一個(gè)可以自定義SQL、存儲(chǔ)過(guò)程和高級(jí)映射的持久層框架。這篇文章主要介紹了MyBatis簡(jiǎn)介與配置MyBatis+Spring+MySql的方法,需要的朋友可以參考下2017-04-04
1秒鐘實(shí)現(xiàn)Springboot?替換/寫(xiě)入?word文檔里面的文字、圖片功能
這篇文章主要介紹了Springboot?替換/寫(xiě)入?word文檔里面的文字、圖片,1秒鐘實(shí)現(xiàn),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
hibernate存取json數(shù)據(jù)的代碼分析
這篇文章主要介紹了hibernate存取json數(shù)據(jù)的代碼分析,需要的朋友可以參考下2017-09-09
spring boot+mybatis 多數(shù)據(jù)源切換(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇spring boot+mybatis 多數(shù)據(jù)源切換(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

