Flask接口签名sign原理与实例代码浅析
479
2022-12-28
Java实现分页代码
在项目中,分页是一个项目中必不可少的,它可以防止我们从数据库中进行大量数据查询时速度变慢,提高我们的查询效率。
1、定义分页模型:PageModel
package com.common.page;
import java.util.List;
/**
* 封装分页信息
* @author Administrator
*
*/
public class PageModel
//结果集
private List
//查询记录数
private int totalRecords;
//每页多少条数据
private int pageSize;
//第几页
private int pageNo;
/**
* 总页数
* @return
*/
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 取得首页
* @return
*/
public int getTopPageNo() {
return 1;
}
/**
* 上一页
* @return
*/http://
public int getPreviousPageNo() {
if (pageNo <= 1) {
return 1;
}
return pageNo - 1;
}
/**
* 下一页
* @return
*/
public int getNextPageNo() {
if (pageNo >= getBottomPageNo()) {
return getBottomPageNo();
}
return pageNo + 1;
}
/**
* 取得尾页
* @return
*/
public int getBottomPageNo() {
return getTotalPages();
}
public List
return list;
}
public void setList(List
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
publicblEMC 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、分页测试:在mysql中建立admin表,里面有字段id、name、password
3、简历Admin的实体bean类:
package com.common.page;
public class Admin {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public blEMCvoid 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、测试调用:
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
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
for(Admin a:list){
System.out.print("ID:"+a.getId()+",用户名:"+a.getName()+",密码:"+a.getPassword());
System.out.println();
}
System.out.print("当前页:"+pageModel.getPageNo()+" ");
System.out.print("共"+pageModel.getTotalPages()+"页 ");
System.out.print("首页:"+pageModel.getTopPageNo()+" ");
System.out.print("上一页:"+pageModel.getPreviousPageNo()+" ");
System.out.print("下一页:"+pageModel.getNextPageNo()+" ");
System.out.print("尾页:"+pageModel.getBottomPageNo()+" ");
System.out.print("共"+pageModel.getTotalRecords()+"条记录");
System.out.println();
}
}
这样分页效果就实现了,我们要实现分页效果,只要传入相应的参数和相应的数据库执行语句即可实现,希望大家能灵活运用。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~