分页的需求:
select from ssm_goods limit 0,3
首页
上一页
下一页
尾页
起始索引位置 = (当前页-1)分页单位
分页单位
当前页页码值
技巧:当有多个参数需要传到持久层时,可以创建集合,将参数封装到集合,将集合传过去。
分页逻辑:
//接收当前页的页码值
if (currentPage == null || currentPage.equals("")){
currentPage = "1";
}else {
System.out.println("当前页的页码值为 "+currentPage);
}
//计算上一页的页码值
Integer prevPage = 0;
if (currentPage.equals("1")){
prevPage = 1;
}else {
prevPage = Integer.parseInt(currentPage) - 1;
}
//计算尾页的值
Integer lastPage = 0;
//获取商品的总记录条数
Integer count = goodsService.getCount();
System.out.println("总记录条数为 "+count);
//指定分页单位
Integer pageSize = 2;
if (count % pageSize == 0){
lastPage = count / pageSize;
}else{
lastPage = (count / pageSize) + 1;
}
//计算下一页的值
Integer nextPage = 0;
if (currentPage.equals(lastPage+"")){
nextPage = lastPage;
}else {
nextPage = Integer.parseInt(currentPage) + 1;
}
//计算起始索引位置
Integer startIndex = (Integer.parseInt(currentPage) - 1) * pageSize;
//将分页所需要的条件传到集合里
hashMap.put("startIndex",startIndex);
hashMap.put("pageSize",pageSize);
分页工具类:
package com.ssm.shop.utils;
import org.springframework.stereotype.Component;
@Component
public class PageUtils {
//当前页
private String currentPage;
//上一页
private Integer prevPage;
//下一页
private Integer nextPage;
//尾页(也可以表示总页数)
private Integer lastPage;
//总记录条数
private Integer count;
//起始索引位置
private Integer startIndex;
//分页单位
private Integer pageSize;
public String getCurrentPage() {
return currentPage;
}
public void setCurrentPage(String currentPage) {
this.currentPage = currentPage;
}
public Integer getPrevPage() {
return prevPage;
}
public void setPrevPage(Integer prevPage) {
this.prevPage = prevPage;
}
public Integer getNextPage() {
return nextPage;
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getLastPage() {
return lastPage;
}
public void setLastPage(Integer lastPage) {
this.lastPage = lastPage;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getStartIndex() {
return startIndex;
}
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
//计算上一页的值
public void initPrevPage(){
prevPage = currentPage.equals("1") ? 1 : Integer.parseInt(currentPage) - 1;
}
//计算尾页的值
public void initLastPage(){
lastPage = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
}
//计算下一页的值
public void initNextPage(){
nextPage = currentPage.equals(lastPage+"") ? lastPage : Integer.parseInt(currentPage) + 1;
}
//计算起始索引位置
public void initStartIndex(){
startIndex = (Integer.parseInt(currentPage) - 1) * pageSize;
}
//初始化的方法
public void init(String currentPage,Integer pageSize,Integer count){
if (currentPage == null || currentPage.equals("")){
currentPage = "1";
}
this.currentPage = currentPage;
this.pageSize = pageSize;
this.count = count;
}
//调用的方法
public void initPage(String currentPage,Integer pageSize,Integer count){
init(currentPage,pageSize,count);
initStartIndex();
initPrevPage();
initLastPage();
initNextPage();
}
}