Java分页插件--PageHelper

Maven依赖

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

这是一个github上的开源项目,可以方便的完成Java web项目中的翻页问题

使用Pageahelper

1.配置拦截器插件

  • 在mybatis的配置文件中配置拦截器插件
<!-- 
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

2.在java类中使用PageInfo来包装查询结果

public String getEmps(@RequestParam(value = "pn",defaultValue="1")Integer pn,
                          Model model){
        PageHelper .startPage(pn,5);
        List<Employee> list = employeeService.getAll();
        //pageinfo里面封装了详细的信息,包括我们查询出来的数据。在构造函数里面的第二个参数,是每页连续显示数量
        PageInfo page = new PageInfo(list,5);
        model.addAttribute("pageInfo",page);
        return "list";
    }

使用pageinfo来包装查询出来的结果,再使用model返回给页面。
在页面我们可以使用page.getNativegatpageNums()方法来获取连续显示页面数

posted @ 2017-09-17 23:26  我想和这个世界聊聊  阅读(2687)  评论(0编辑  收藏  举报