第一步:Pagehelper 依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>

第二步:CommentMapper.xml

<sql id="Base_Column_List">
    comment_id, content,blog_id,nick_name,avatar,email,reply_nick_name, pid, create_time, site_url,page,is_check
</sql>

<select id="getComment" resultMap="comment">
    select
    <include refid="Base_Column_List"/>
    from article_comment
    where pid = #{comment_id} and is_check=1
</select>

第三步:PageHelper 分页查询

PageHelper 类参数说明:

  • currentPage:当前页数、第几页数
  • pageSize:每页显示记录数

简单的分页查询

@Controller
public class CommentController{   
   
    @Autowired
    private CommentService commentService;
    
    @Autowired
    CommentMapper commentMapper;
    
    @PostMapping("/comment/query")
    @ResponseBody   
    public CommentQuery getComment(@RequestBody CommentQuery commentQuery int currentPage,int pageSize) {
        //设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】
        PageHelper.startPage(currentPage, pageSize);
        List<Comment> listComment = commentMapper.getComment(commentQuery); //根据条件进行查询
        return listComment;  
    }
}

PageHelper.startPage(pageNo,pageSize) 只对其后的第一个查询有效

PageHelper.startPage(pageNo,pageSize); 只对其后的第一个查询有效。如把代码改为下面这样,第一个查询执行分页查询,第二个查询并没有分页。

@PostMapping("/comment/query")
@ResponseBody   
public CommentQuery getComment(@RequestBody CommentQuery commentQuery int currentPage,int pageSize) {
    PageHelper.startPage(currentPage, pageSize);						//PageHelper 只对其后的第一个查询有效 
    List<Comment> listComment = commentMapper.getComment(commentQuery); //执行分页查询
    
    List<Comment> listComment2 = commentMapper.getComment(commentQuery);//不会执行分页查询
    //要想继续执行分页查询,需要再次调用PageHelper.startPage
    
    return listComment;  
}
示例1

如果想取出分页信息,可以强制转换为Page

@Controller
public class CommentController{   
   
    @Autowired
    private CommentService commentService;
    
    @Autowired
    CommentMapper commentMapper;
    
    @PostMapping("/comment/query")
    @ResponseBody   
    public CommentQuery getComment(@RequestBody CommentQuery commentQuery int currentPage,int pageSize) {
        
        PageHelper.startPage(currentPage, pageSize);
        List<Comment> listComment = commentMapper.getComment(commentQuery); //根据条件进行查询
        
        //分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>
        
        System.out.println(((Page) listComment).getTotal());//总条数
        System.out.println(((Page) listComment).getList());//显示的数据
        
        return listComment;  
    }
}        
示例2

使用PageInfo的用法,PageInfo 是 Pagehelper中内的分页的信息类,也可以自定义分页信息类

@Controller
public class CommentController{   
   
    @Autowired
    private CommentService commentService;
    
    @Autowired
    CommentMapper commentMapper;
    
    @PostMapping("/comment/query")
    @ResponseBody   
    public CommentQuery getComment(@RequestBody CommentQuery commentQuery int currentPage,int pageSize) {
        
        PageHelper.startPage(currentPage, pageSize);
        List<Comment> listComment = commentMapper.getComment(commentQuery); //根据条件进行查询
        
        //用PageInfo对结果进行包装
        PageInfo info=new PageInfo(listComment);//1、PageInfo 是 pagehelper中内值的分页的信息类
        
		//PageInfo包含了非常全面的分页属性
        //测试PageInfo全部属性        
        System.out.println(info.getTotal());
        System.out.println(info.getList()); 
        System.out.println(info.getPageNum());
        System.out.println(info.getPageSize());
        System.out.println(info.getStartRow());
        System.out.println(info.getEndRow());
        System.out.println(info.getPages());
        System.out.println(info.getFirstPage());
        System.out.println(info.getLastPage());
        System.out.println(info.isFirstPage());
        System.out.println(info.isLastPage());
        System.out.println(info.isHasPreviousPage());
        System.out.println(info.isHasNextPage());
        
        return listComment;  
    }
}

PageInfo全部属性

//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//排序
private String orderBy;
 
//可以在页面中"显示startRow到endRow n条数据"
private int startRow;
private int endRow;
 
 
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集数据
private List<T> list;
 
//第一页
private int firstPage;
//前一页
private int prePage;
//下一页
private int nextPage;
//最后一页
private int lastPage;
 
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐