分页实现原理及model层代码

本文深入探讨了分页算法的实现原理,包括当前页、首页、末页、上一页、下一页的逻辑处理,并详细介绍了如何通过SQL查询获取指定页面的数据。同时,展示了与数据库交互的Java代码实现,包括查询总记录数、当前页记录信息等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分页怎么实现:
看到效果

1 当前页-1 当前页+1 最后一页 总页数
首页 上一页 下一页 末页
算法分析:------------------------------------------------
当前页:
int nowpage;
首页:
nowpage=1;
末页:
int countpage; 总页
nowpage=countpage;
上一页:
nowpage = nowpage-1;
if(nowpage<1){
nowpage=1;
}
下一页:
nowpage = nowpage+1;
if(nowpage>countpage){
nowpage=countpage;
}
-----------------------------------------------------
理解概念:
当前页 nowpage
总页数 countpage =======7页
每页显示的记录数 10条
当前页开始的记录数:(nowpage-1)*10+1;
1 1-10
2 11-20
3 21 30
4 31 40
5 41 50
总记录数:
countrecord =64记录
总页数==总记录数%每页显示的记录数==0?总记录数/每页显示的记录数:总记录数/每页显示的记录数+1;
------------------------------------------
表的操作:
总记录数:select count(*) from 表名;
每页显示3条记录: 声明 int pagesize=3;
总页数:总页数==总记录数%每页显示的记录数==0?总记录数/每页显示的记录数:总记录数/每页显示的记录数+1;
当前页的记录信息:
select * from 表名 limit (nowpage-1)*pagesize,pagesize;

package qi.dao;

import java.util.List;

import qi.domain.Admin;

public interface AdminDao {
List<Admin>findNowPageInfo(Integer nowpage);
Integer findCountRecord();
Integer findCountPage();

}


package qi.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import qi.domain.Admin;

public class AdminDaoImpl implements AdminDao {

private static Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private static final String URL = "jdbc:mysql://localhost:3306/3g?user=root&password=bwosi1nea&useUnicode=true&characterEncoding=UTF8";

static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(URL);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static final Integer PAGESIZE=3;
private Integer countRecord;
private Integer countPage;
public List<Admin> findNowPageInfo(Integer nowpage) {
List<Admin> allentites = new ArrayList<Admin>();
String sql = "select id,name,pass,sex,age from admin limit ?,?";
try {
pstmt = conn.prepareStatement(sql);
int index=1;
pstmt.setInt(index++,(nowpage-1)*PAGESIZE);
pstmt.setInt(index++,PAGESIZE);
rs = pstmt.executeQuery();
while (rs.next()) {
Admin entity = new Admin();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setPass(rs.getString("pass"));
entity.setSex(rs.getString("sex"));
entity.setAge(rs.getInt("age"));

allentites.add(entity);
}
release(rs, pstmt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return allentites;
}
public Integer findCountRecord() {
String sql="select count(*) from admin";
try {
pstmt = conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){

this.countRecord = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.countRecord;
}
private void release(ResultSet rs, PreparedStatement pstmt) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
public Integer findCountPage() {
findCountRecord();
this.countPage=this.countRecord%this.PAGESIZE==0?this.countRecord/this.PAGESIZE:this.countRecord/this.PAGESIZE+1;
return this.countPage;
}
}


package qi.domain;
public class Admin {

private Integer id;
private String name;
private String pass;
private String sex;
private Integer age;
public Admin() {
super();
// TODO Auto-generated constructor stub
}
public Admin(Integer id, String name, String pass, String sex, Integer age) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
return "Admin [id=" + id + ", name=" + name + ", pass=" + pass
+ ", sex=" + sex + ", age=" + age + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

}

package qi.junit;

import java.util.List;

import org.junit.Test;

import qi.domain.Admin;
import qi.service.AdminServiceImpl;


public class AdminTest {
private AdminServiceImpl aService = new AdminServiceImpl();

@Test
public void getCountRecrd(){
System.out.println(aService.findCountRecord());
}
@Test
public void getCountPage(){
System.out.println(aService.findCountPage());
}
@Test
public void NowPageInfo(){
List<Admin> admins = aService.findNowPageInfo(4);
for(Admin admin:admins){
System.out.println(admin.toString());
}
}
}


package qi.service;

import qi.dao.AdminDao;

public interface AdminService extends AdminDao{

}


package qi.service;

import java.util.List;

import qi.dao.AdminDao;
import qi.dao.AdminDaoImpl;
import qi.domain.Admin;

public class AdminServiceImpl implements AdminService{
private AdminDao aDao = new AdminDaoImpl();
public List<Admin> findNowPageInfo(Integer nowpage) {
return aDao.findNowPageInfo(nowpage);
}

public Integer findCountRecord() {
return aDao.findCountRecord();
}

public Integer findCountPage() {
return aDao.findCountPage();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值