PageHelper的使用

自己基于ssm框架使用pagehelper分页的使用总结

一、引入依赖


    com.github.pagehelper
    pagehelper
   5.1.2

二、在mybatis配置文件中配置分页插件


  		  


三、创建实体类PageBean存储分页设定信息

public class PageBean {
	 private int start; //开始页数
	    private int count; //每页显示个数
	    private int total; //总个数
	    private String param; //参数
	 
	    private static final int defaultCount = 5; //默认每页显示5条
	 
	    public int getStart() {
	        return start;
	    }
	    public void setStart(int start) {
	        this.start = start;
	    }
	    public int getCount() {
	        return count;
	    }
	    public void setCount(int count) {
	        this.count = count;
	    }
	 
	    public PageBean (){
	        count = defaultCount;
	    }
	    public PageBean(int start, int count) {
	        this();
	        this.start = start;
	        this.count = count;
	    }
	 
	    public boolean isHasPreviouse(){
	        if(start==0)
	            return false;
	        return true;
	    }
	    public boolean isHasNext(){
	        if(start==getLast())
	            return false;
	        return true;
	    }
	 
	    public int getTotalPage(){
	        int totalPage;
	        // 假设总数是50,是能够被5整除的,那么就有10页
	        if (0 == total % count)
	            totalPage = total /count;
	            // 假设总数是51,不能够被5整除的,那么就有11页
	        else
	            totalPage = total / count + 1;
	 
	        if(0==totalPage)
	            totalPage = 1;
	        return totalPage;
	 
	    }
	 
	    public int getLast(){
	        int last;
	        // 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
	        if (0 == total % count)
	            last = total - count;
	            // 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
	        else
	            last = total - total % count;
	        last = last<0?0:last;
	        return last;
	    }
	 
	    @Override
	    public String toString() {
	        return "Page [start=" + start + ", count=" + count + ", total=" + total + ", getStart()=" + getStart()
	                + ", getCount()=" + getCount() + ", isHasPreviouse()=" + isHasPreviouse() + ", isHasNext()="
	                + isHasNext() + ", getTotalPage()=" + getTotalPage() + ", getLast()=" + getLast() + "]";
	    }
	    public int getTotal() {
	        return total;
	    }
	    public void setTotal(int total) {
	        this.total = total;
	    }
	    public String getParam() {
	        return param;
	    }
	    public void setParam(String param) {
	        this.param = param;
	    }
}

四、Controller编写

	@RequestMapping("blog")
	public String blog(Model model,PageBean page){
		//设置每页显示几条
		PageHelper.offsetPage(page.getStart(),5);
		List list = blogService.selectAll();
		//设置总页数
		int total = (int)new PageInfo<>(list).getTotal();
		page.setTotal(total);
	
		model.addAttribute("page",page);
		model.addAttribute("blogList",list);
		
		return "blog";
	}

五、前台分页显示

数据展示部分

 
	 
        ${blog.pid}
	 ${blog.title}
	${blog.author}
	${blog.intime}
	${blog.uptime}
			           
	
	修改
	 删除
	
			           
	
 


效果:

PageHelper的使用_第1张图片


你可能感兴趣的:(PageHelper的使用)