package cn.td.service.impl;
import cn.td.dao.impl.Dao;
import cn.td.domain.PageBean;
import cn.td.domain.User;
import java.util.List;
/**
* 分页
*/
public class Service {
private Dao dao = new Dao();
public PageBean<User> findUserByPage(String _currentPage, String _rows) {
int currentPage = Integer.parseInt(_currentPage);
int rows = Integer.parseInt(_rows);
//如果当前页码小于或等于0页,设置当前页码参数为第一页
if(currentPage <=0) {
currentPage = 1;
}
//1.调用dao查询总记录数
int totalCount = dao.findTotalCount();
//2.计算总页码
int totalPage = (totalCount % rows) == 0 ? (totalCount/rows) : (totalCount/rows) + 1;
//如果当前页码大于总页码,设置当前页码参数为总页码数
if(currentPage >=totalPage){
currentPage = totalPage;
}
//3.调用dao查询List集合
//计算开始的记录索引
int start = (currentPage - 1) * rows;
List<User> list = dao.findByPage(start,rows);
//4.封装pageBean
PageBean<User> pageBean = new PageBean<User>();
pageBean.setCurrentPage(currentPage);
pageBean.setRows(rows);
pageBean.setTotalCount(totalCount);
pageBean.setList(list);
pageBean.setTotalPage(totalPage);
//返回pageBean
return pageBean;
}
}
分页后台首页,尾页限制(服务层)
最新推荐文章于 2021-07-21 09:40:58 发布