后端分页神器,mybatis pagehelper 在SSM与springboot项目中的使用

mybatis pagehelper想必大家都耳熟能详了,是java后端用于做分页查询时一款非常好用的分页插件,同时也被人们称为mybatis三剑客之一,下面 就给大家讲讲如何在SSM项目和springboot项目中使用mybatis pagehelper

一、mybatis pagehelper在SSM项目中的使用


1.引入maven依赖,(自行选择版本,这里我用的4.1.3)



2.在mybatis的配置文件中进行配置


<plugins>
   <!--pageNum当前页数 pageSize 每页显示的记录数,pages 总页数 totals总记录数 -->
   <!-- com.github.pagehelper为PageHelper类所在包名 -->
   <plugin interceptor="com.github.pagehelper.PageHelper">
      <!--设置数据库方言,这里我用的mysql-->
      <property name="dialect" value="mysql"/>
      <!-- 该参数默认为false -->
      <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
      <!-- 和startPage中的pageNum效果一样-->
      <property name="offsetAsPageNum" value="true"/>
      <!-- 该参数默认为false -->
      <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
      <property name="rowBoundsWithCount" value="true"/>
      <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
      <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
      <property name="pageSizeZero" value="true"/>
      <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
      <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
      <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
      <property name="reasonable" value="false"/>
   </plugin>
</plugins>


3.使用mybatis分页插件

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 分页查询视频列表
 * @param pageNum
 * @param pageSize
 * @return JqueryGrid
 */
@Transactional(propagation = Propagation.SUPPORTS)
public JqueryGrid selectVideoListVo(Integer pageNum, Integer pageSize){
    //开始使用mybatis 分页插件
    PageHelper.startPage(pageNum,pageSize);
   List<VideoVo> videoVoList =  videosMapper.selectVideoListVo();
    PageInfo<VideoVo> pageInfo = new PageInfo<VideoVo>(videoVoList);
    JqueryGrid jqueryGrid = new JqueryGrid();
    //设置当前页
    jqueryGrid.setPage(pageInfo.getPageNum());
    //设置总页数
    jqueryGrid.setTotal(pageInfo.getPages());
    //设置总记录数
    jqueryGrid.setRecords(pageInfo.getTotal());
    //设置当前页的list集合
    jqueryGrid.setRows(pageInfo.getList());
    return jqueryGrid;
}
在该方法中,传入了当前页 (pageNum),以及每一页显示的数量(pageSize),我们对遍历的列表再进行分页查询,得到最终想要的结果。


二、mybatis pagehelper在springboot项目中的使用


1.导入maven依赖

                    <!--pagehelper 用于springboot项目-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>

</dependency>


2.在applicationContext.xml或applicationContext.yml中进行配置



3.使用mybatis分页插件

        import com.github.pagehelper.PageHelper;
        import com.github.pagehelper.PageInfo;

       @Transactional(propagation = Propagation.SUPPORTS)
public PagedResult selectMyFollow(Integer pageNum,String userId,Integer pageSize) {
if(pageNum==null||pageNum<=0) {
pageNum=1;
}
//进行分页
PageHelper.startPage(pageNum, pageSize);
//查询到该用户发布的所有的视频

List<Users> userList =usersMapper.selectMyFollow(userId);

               //

PageInfo<Users> pageInfoList = new PageInfo<Users>(userList);
PagedResult pagedResult = new PagedResult();
//当前页数
pagedResult.setPageNum(pageInfoList.getPageNum());
//总页数
pagedResult.setPages(pageInfoList.getPages());
//总记录数
pagedResult.setTotals(pageInfoList.getTotal());
//设置当前页的列表内容
pagedResult.setPageList(pageInfoList.getList());
return pagedResult;

}

    该方法返回的就是一个分页查询之后封装的一个对象。

==========================================================================

在SSM和springboot项目中引入mybatis pagehelper的方法就介绍到这里了。

PageInfo(com.github.pagehelper.PageInfo)类中有几个属性,其中 pageNum表示当前页,pages表示总页数,total表示总记录数,List表示当前页的列表内容。








猜你喜欢

转载自blog.csdn.net/ajklaclk/article/details/80738004