该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
SSM分页查询
后台
1. 实体
package com.ssm.entity;
import java.util.List;
public class PageBean {
private int currentPage;//当前页
private int pageSize;//每页显示的条数
private int totalCount;//数据总数
private int totalSize;//总页数
private List list;//存放返回的数据
public PageBean(){
currentPage = 1;
pageSize = 5;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
2. mapper接口
//分页
public List findAll(Map map);
3. mapper.xml
select * from users limit #{start},#{end}
4. service
//分页
public PageBean findAll(int currentPage);
5. serviceImpl
public PageBean findAll(int currentPage) {
PageBean pb = new PageBean();
Map map =
new HashMap();
if(currentPage<=0){
currentPage = 1;
}
//获取总条数
int totalCount = uMapper.dataCount();
//添加总条数
pb.setTotalCount(totalCount);
//计算总页数
int totalSize=totalCount/pb.getPageSize();
if(totalCount%pb.getPageSize()!=0){
totalSize+=1;
}
//判断当前页是否大于总页数
if(currentPage>=totalSize){
currentPage=totalSize;
}
//添加当前页
pb.setCurrentPage(currentPage);
//添加总页数
pb.setTotalSize(totalSize);
//设置起始值和结束值
map.put("start", (currentPage-1)*pb.getPageSize());
map.put("end", pb.getPageSize());
//获取分页数据
List lu=uMapper.findAll(map);
//赋值给pagebean
pb.setList(lu);
return pb;
}
6. 控制器
@RequestMapping("/fymapping")
public ModelAndView fy(@RequestParam(value="currentPage",defaultValue="1") int currentPage){
ModelAndView mv = new ModelAndView("jsp/fy.jsp");
//当前页不能小于等于0
//如果小于等于0当前页等于1
if(currentPage<=0){
currentPage=1;
}
PageBean pb=uService.findAll(currentPage);
//控制路径中的页数
if(currentPage>=pb.getCurrentPage()){
currentPage= pb.getCurrentPage();
}
int[] index = new int[pb.getTotalSize()];
for (int i = 0; i < index.length; i++) {
index[i]=i+1;
}
mv.addObject("currentPage", currentPage);
mv.addObject("totalSize", index);
mv.addObject("ulist", pb.getList());
return mv;
}
7.访问 页面
8. 结果页面