PageHelper分页插件的原理及其使用

PageHelper分页插件原理

PageHelper本身是一个物理分页插件,实际原理就是修改最后的执行sql,增加相应的分页内容,是基于拦截器实现的。

例如,首先你配置了PageHelper的Pagenum和Pagesize属性,调用完startPage后,他会通过PageInterceptor对其后的第一个执行sql进行拦截,也就是对List<User> list = userService.findAllUser()进行拦截,这里原本的sql可能是 select * from users,他会自动拼接上分页的sql语句,比如mysql环境的话,就是拼接上limit语句,随后执行,最后的结果,可以通过PageInfo和Page进行获取。

 

pom.xml文件添加依赖

<!-- mybatis分页依赖start============= -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>
    <!-- mybatis分页依赖end============= -->

 

  spring-mybatis.xml 文件关于整合mybatis的配置(分页插件有标注)


    <!--整合mybatis -->
    <!-- 1 注入一股mybatis的sqlsessionFactory这就是我们所要做的关键步骤 2 声明式的事务管理 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 引入mappers文件 -->
        <!-- <property name="configLocation" value="classpath:mybatis-config.xml"></property> -->
        <!-- 这就要求所有的mapper文件必须在com/sz/mapper/之下 -->
        <property name="mapperLocations" value="classpath:com/sz/mapper/**/*.xml" />
        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
                <!-- 可以加入驼峰命名,其它mybatis的配置也就是mybatis.cfg.xml的相关配置都会转移到这里来
                 user_name userName
                 -->
                <property name="mapUnderscoreToCamelCase" value="true" />
            </bean>

        </property>
        <!-- 插件配置 -->
        <property name="plugins">
            <array>

                <!-- 分页插件的配置 拦截器实现分页功能 -->
                <bean class="com.github.pagehelper.PageInterceptor">
                <!-- 分页插件的配置 拦截器实现分页功能 -->

                    <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

页面代码 main.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品信息管理系统</title>
    <link rel="stylesheet" href="${ctx}/static/plugins/layui/css/layui.css">
</head>
<body>
<form>
    <table class="layui-table">
        <thead>
        <tr>
            <th >商品编号</th>
            <th width="30px;">商品名称</th>
            <th>商品价格(单位)</th>
            <th>库存数量</th>
            <th>订单状态</th>
            <th>商品区域(1.朝阳2.海淀3.丰台4.西城5.昌平)</th>
            <th>创建时间</th>
            <th>操作</th>  <th>操作</th>  <th>操作</th>

            <%--<th>操作</th>--%>
        </tr>
        </thead>
        <%--  三大排序要求  数量的降序,日期的升序,价格降序 --%>
        <tbody>
        <c:forEach items="${page.list}" var="ob">
            <tr>
                <td>${ob.id}</td>
                <td width="30px;">${ob.goodsName}</td>
                <td>
                        ${ob.goodsPrice}
                </td>

                <td>${ob.goodsCount}</td>
                <td>${ob.billStatus}</td>
                <td>${ob.goodsDistrict}</td>



                <td>${ob.creationTime}</td>
                <%-- 根据id查询返回 修改页面--%>
                <td><a href="${ctx}/good/query/by/${ob.id}">修改</a></td>
                <td><a href="${ctx}/good/delete/${ob.id}">删除</a></td>
                <td><a href="${ctx}/jsp/add.jsp">新增</a></td>


            </tr>

        </c:forEach>
        <tr>
            <td> 共${page.total} 条记录 第 ${page.pageNum} / ${page.pages}页</td>
            <td colspan="8"> <a href="${ctx}/good/pageTo/${page.pageNum=1}">首页</a>
                <a href="${ctx}/good/pageTo/${page.pageNum-1}">上一页</a>
                <a href="${ctx}/good/pageTo/${page.pageNum+1}">下一页</a>
                <a href="${ctx}/good/pageTo/${page.pages}">尾页</a>
            </td>
        </tr>



        </tbody>
    </table>
    <a href="${ctx}/good/select">点击另一个查询页面</a>
    <p> <a href="${ctx}/good/page"> 返回商品详情首页</a></p>
</form>
<%-- 导入前端框架--%>
<script src="${ctx}/static/plugins/layui/layui.js"></script>
<script>
   $(function () {

       alert(${ob.id});
   })
</script>
</body>
</html>

接口 GoodsService.java

package com.sz.service;

import com.github.pagehelper.PageInfo;
import com.sz.pojo.Goods;

import java.util.List;

public interface GoodsService {
   
//     提供分页插件的查询

    PageInfo<Goods> queryByPage(PageInfo pageInfo);

}

实现类 GoodsServiceImpl

package com.sz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sz.mapper.GoodsMapper;
import com.sz.pojo.Goods;
import com.sz.service.GoodsService;
import constant.CommonCodeConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

  @Override
    public PageInfo<Goods> queryByPage(PageInfo pageInfo) {

        // 去第几页, 页码的大小
        PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize());
        List<Goods> lo = goodsMapper.queryAll();
        PageInfo<Goods> page = new PageInfo<Goods>(lo);

        return page;
    }
}

控制器代码GoodsController

package com.sz.controller;

import com.github.pagehelper.PageInfo;
import com.sz.pojo.Goods;
import com.sz.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Date;


import java.util.List;

@Controller
@RequestMapping("/good")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

//    查询商品详情并带有分页参数

    @RequestMapping("/page")
    public String queryByPage(Model model) {
        PageInfo<Goods> pageInfo = new PageInfo<>();
        pageInfo.setPageSize(3); //每页的数量
        pageInfo.setPageNum(1);//当前页
        pageInfo = goodsService.queryByPage(pageInfo);
        model.addAttribute("page", pageInfo);


        return "main";
    }

    //    跳转分页页面
    @RequestMapping("/pageTo/{page}")
    public String pageInfo(@PathVariable("page") Integer page, Model model) {

//        上一页到顶时
        if (page < 1) {
            page = 1;
        }

        PageInfo<Goods> pageInfo = new PageInfo<>();

        pageInfo.setPageSize(3); //每页的数量


        pageInfo.setPageNum(page);//当前页
        pageInfo = goodsService.queryByPage(pageInfo);
        model.addAttribute("page", pageInfo);
        return "main";


    }

}

关键要素:

通过 pageInfo.setPageSize(int);设置每页显示的数量

         pageInfo.setPageNum(page);设置当前所在的页数

 

希望通过简洁有效的方式实现分页效果

提供项目代码:https://pan.baidu.com/s/1Ji0SYhc_Rg9ALcJiJa01Zg 
提取码:da3j

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值