pageHelper插件 --- 简单分页

版权声明:Unruly https://blog.csdn.net/weixin_41660948/article/details/85248468

前言

化繁为简,基本使用;对此插件进行基本分页使用即可。具体更多参考官方的API

1、Maven搭建引入依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
</dependency>

2、Spring整合方式

配置一些运行时参数:
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                  <!--使用下面的方式配置参数,一行配置一个 -->
                  <value>
                    reasonable=true
                    autoRuntimeDialect=true
                  </value>
                </property>
            </bean>
        </array>
    </property>
</bean>

3、使用方式

  1. 调用PageHelper.starPage(页码,显示数)
  2. 获取查询的数据集合List;
  3. 将集合放入 PageInfo<集合类型> 容器中;
  4. 返回容器。
	@RequestMapping("/index.html")
	public String goToIndex(
			@RequestParam(name="pageNum",defaultValue="1")Integer pageNum,
			ModelMap model) {
    // 开始分页
		PageHelper.startPage(1,5);
    
		// 查询所有需要分页的数据
		List<Books> books = bookBiz.findAll();
    
		// 将数据交给pageInfo容器管理
		PageInfo<Books> page = new PageInfo<>(books);
    
		// 返回容器对象
		model.addAttribute("page",page);
		return "index";
	}

4、页面获取方式

<c:if test="${requestScope.page.list != null }">
    <c:forEach items="${requestScope.page.list }" var="item">
        <tr>
            <td>${item.bookName }</td>
            <td>${item.bookAuthor }</td>
            <td>${item.bookPublish }</td>
            <td>${item.bookPage }</td>
            <td>${item.bookPrice }</td>
        </tr>
    </c:forEach>
</c:if>

<a href="index.html?pageNum=${page.firstPage}">首页</a> 
<a href="index.html?pageNum=${page.prePage}">上一页</a> 
<a href="index.html?pageNum=${page.nextPage}">下一页</a> 
<a href="index.html?pageNum=${page.lastPage}">尾页</a>
<span>第${page.pageNum}页/共${page.pages}页</span>

5、分页容器的常用属性

使用Maven搭建项目查看PageInfo容器源码可以看到属性意思。

在这里插入图片描述

6、示例图

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41660948/article/details/85248468