MyBatis插件PageHelper的使用心得

本文详细介绍了在SSM框架中使用PageHelper分页插件的方法,包括配置步骤、自定义SQL语句及前端分页实现,适用于初学者快速掌握分页功能。

        在实习中的项目用到了SSM框架。在项目中分页功能的使用非常频繁,自己写的针对个别性的分页代码不适合复用而又因为自己太菜了没办法自己写出复用的分页代码。在了解到Mybatis的分页插件PageHelper后,我前往学习并结合实习的项目,完成了分页。以下是我对pageHelper的理解以及使用的方法。

      开发环境为IDEA。

配置

    1.在POM.XML文件中配置pageHelper依赖

<properties>
        <pagehelper-version>5.0.0</pagehelper-version>
        <jsqlparser-version>0.9.5</jsqlparser-version>
    </properties>
<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper-version}</version>
        </dependency>

        <!-- pagehelper的依赖包:jsqlparser -->
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>${jsqlparser-version}</version>
        </dependency>

2.在mybatis-config.xml配置PageHelper

    <!-- 配置mybatis的分页插件PageHelper -->
    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
            <property name="properties" value="mysql"/>
        </plugin>
    </plugins>

使用

      我在使用PageHelper的时候是自定义sql语句的。DAO包中的sql语句:

    public  String SelectNews(){
        String sql="Select * from   `ums`.`news` ";
        return sql;
    }

    再在mapper包中自定义一个Mapper,实现对该sql语句的对应。

public interface CustomedNewsMapper {
    @SelectProvider(method="SelectNews",type = com.scau.ums.util.dao.NewsSql.class)
    public List<News> SelectNews();

}

   在Service层实现该sql语句方法。

@Service
public class newsImp {
    //自定义mapper文件
    @Autowired
    public CustomedNewsMapper customedNewsMapper;
    
    public List<News> SelectNews(Integer currentPage){
        PageHelper.startPage(currentPage,5);
        return  customedNewsMapper.SelectNews();
    }
}

  在controller层中调用Service的Select方法。

    @Autowired
    private newsImp newsImp;

    @RequestMapping(value="/showNews")
    public  String  showNews(HttpServletRequest request, @RequestParam(value =    "currentPage",required=false,defaultValue="1")Integer currentPage){

        List<News> NewsList = newsImp.SelectNews(currentPage);
        PageInfo<News> pageInfo= new PageInfo<News>(NewsList);

        request.setAttribute("NewsList",NewsList);
        request.setAttribute("pageInfo",pageInfo);

        return "showNews";
    }

将完成Select后获得的List列表填空至PageInfo中。可以获取到一个List以及分页的属性的封装对象,该对象非常好用。可以通过PageInfo.getList()来获取List,也可以通过get方法获得其他分页属性。

在jsp页面实现分页功能。

<%@ page import="com.scau.ums.model.News" %>
<%@ page import="java.util.List" %>
<%@ page import="com.github.pagehelper.PageInfo" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>showNews</title>
</head>
<script>


</script>


<body>

<%
    List<News> NewsList = (List<News>) request.getAttribute("NewsList");
    PageInfo infoPager= (PageInfo) request.getAttribute("pageInfo");
%>
<%--    遍历新闻列表并输出--%>

<%

    for(int i=0;i<NewsList.size();i++){
        News news=NewsList.get(i);
%>
<a href="specifiedNews?NewsId=<%=news.getId()%>"><%=news.getTitle()%></a>
<br>
<%
    }
%>


<div class="page text-right clearfix" style="margin-top: 40px">
    <!-- 当前页为第一页时href="javascript:void(0)" 禁用 a 标签的点击时间事件 
      当前页不是第一页时请求url 中返回currentPage=pageInfo.pageNum - 1  当前页 -1-->
    <a <c:if test="${pageInfo.pageNum != pageInfo.firstPage}">href="showNews?currentPage                                          =${pageInfo.pageNum - 1 }"</c:if>
       <c:if test="${pageInfo.pageNum == pageInfo.firstPage}">href="javascript:void(0)" class="disabled"</c:if> 
    >上一页</a>

    <!-- foreach 从 1 开始 到 总页数结束  遍历输出 -->
    <c:forEach begin="1" end="${pageInfo.pages }" varStatus="status">
        <a href="showNews?currentPage=${status.count }"
           <c:if test="${status.count == pageInfo.pageNum}">class="select"</c:if>>${status.count }</a>
    </c:forEach>
    <!-- 当前页为最后一页时href="javascript:void(0)" 禁用 a 标签的点击时间事件 
        当前页不是最后一页时请求url 中返回currentPage=${pageInfo.pageNum - 1 } 当前页 -1-->
    <a <c:if test="${pageInfo.pageNum == pageInfo.lastPage}">class="disabled" href="javascript:void(0)"</c:if>
       <c:if test="${pageInfo.pageNum != pageInfo.lastPage}">href="showNews?currentPage=${pageInfo.pageNum + 1 }"</c:if> 
    >下一页</a>
</div>


</body>
</html>

以上是我的前端代码,使用到了C标签所以也需要加入C标签的引用。其实在前后台都可以只set和get   pageInfo对象,然后通过pageInfo.getList()来获取NewsList。注意href要换成自己的跳转页面URL。

 

整个的工作流程主要为 在Controller中调用Service的get方法。在Service中,其get方法是通过自定义Mapper的自定义sql语句来实现select的。将PageInfo创给前端,由PageInfo的属性决定分页的情况。需要注意的是,每页展示的数量是在startPage语句中的pageSize决定的。

使用的步骤大致如上。下面来说说我在使用过程中的心得。

心得

首先,需要注意的点有以下几点:

1.在sql语句中无需添加limit和count语句,只需要填写好自己的条件语句就好。PageHelper会自动执行sql语句的count和Limit条件语句。再填写count和limit会报错。如下图日志,其中在数据库中news共有29条。

2.在执行自定义Mapper接口的select方法前需要进行PageHelper.startPage(currentPage,PageSize)的配置。配置好后默认为接下来的Select语句分页处理,即自动附上count和limit。currentPage指当前页,PageSize指每页展示的数量。

3.在需要用到倒序或者其他条件的Select时,在自定义Select语句填写好限制条件就好。但是不需要添加limit和count,PageHelper会自动实现。

 

参考链接:https://blog.youkuaiyun.com/qq_39126966/article/details/81805904

https://blog.youkuaiyun.com/qq_33609401/article/details/83749083

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值