帮我写一个分页的java代码
时间: 2023-04-06 17:04:39 浏览: 142
好的,我可以回答这个问题。以下是一个简单的Java分页代码示例:
```java
public class Pagination {
private int pageSize; // 每页显示的记录数
private int currentPage; // 当前页码
private int totalRecords; // 总记录数
public Pagination(int pageSize, int currentPage, int totalRecords) {
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
public int getStartIndex() {
return (currentPage - 1) * pageSize;
}
public int getEndIndex() {
int endIndex = currentPage * pageSize - 1;
return endIndex < totalRecords ? endIndex : totalRecords - 1;
}
}
```
这个类可以用来计算分页相关的参数,例如总页数、起始记录索引、结束记录索引等。你可以根据自己的需求进行修改和扩展。
阅读全文