0
点赞
收藏
分享

微信扫一扫

MyBatis实用场景-PageHelper插件进行分页

PageHelper是MyBatis中非常方便的第三方分页插件。 官方文档: https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md

使用步骤 依赖/导包

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

springmvc配置

<configuration>
    <!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

</configuration>

使用

public class TestPageHelper {

    @Test
    public void testPageHelper() {
        // 创建一个spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*");
        // 从spring容器中获取mapper代理对象
        TbItemMapper mapper = context.getBean(TbItemMapper.class);
        // 执行查询并分页,TbItemExample是逆向工程自动生成的,用来进行条件查询,这里不设置则表示无条件
        TbItemExample example = new TbItemExample();
        //分页处理,显示第一页的10条数据
        PageHelper.startPage(1, 10);
        List<TbItem> list = mapper.selectByExample(example);//查询
        // 取商品列表
        for(TbItem item : list) {
            System.out.println(item.getTitle());
        }
        // 取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list);
        long total = pageInfo.getTotal(); //获取总记录数
        System.out.println("共有商品信息:" + total);
    }
}

springboot 配置

## pagehelper分页插件配置 ##
#标识是哪一种数据库
pagehelper.helperDialect=mysql
#启用合理化,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
pagehelper.reasonable=true
#为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
pagehelper.params=count=countSql
#支持通过 Mapper 接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页
pagehelper.supportMethodsArguments=true
#如果 pageSize=0 就会查询出全部的结果(相当于没有执行分页查询)
pagehelper.page-size-zero=true

使用

  • 我们可以将查询的结果使用﹔将查询的结果放在pageinfo中这个pageInfo就有非常多能够用的
代码片段
 @RequestMapping("/getAll")
    public String getAll(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model){
        //紧跟他的查询就是一个分页查询
        PageHelper.startPage(pn,5);
        List<Teacher> teachers = teacherService.getAll();

        //将查询到的结果使用:将查询到的结果放到pageInfo中,这个pageInfo就有非常多的内容可以用
        //第二个参数是连续要显示的页码
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers,6);
        System.out.println("当前页码:"+pageInfo.getPageNum());
        System.out.println("总页码"+pageInfo.getPages());
        System.out.println("总记录数"+pageInfo.getPageSize());
        System.out.println("当前页有多少条记录"+pageInfo.getSize());
        System.out.println("分页每页显示多少条"+pageInfo.getPageSize());
        System.out.println("获取前一页"+pageInfo.getPrePage());
        System.out.println("获取后一页"+pageInfo.getNextPage());
        System.out.println("获取查询到的list"+pageInfo.getList());
        int[] navigatepageNums = pageInfo.getNavigatepageNums();
        model.addAttribute("pageInfo",pageInfo);
        return "index.jsp";
    }
   <tr>
        <td colspan="4">
            <a rel="nofollow" href="/getAll?pn=1">首页</a>
            <a rel="nofollow"  href="/getAll?pn=${pageInfo.prePage}">上一页</a>
           <c:forEach items="${pageInfo.navigatepageNums}" var="p">
               <a rel="nofollow" href="/getAll?pn=${p}">${p}</a>
           </c:forEach>
            <a rel="nofollow"  href="/getAll?pn=${pageInfo.nextPage}">下一页</a>
            <a rel="nofollow" href="/getAll?pn=${pageInfo.pages}">尾页</a>
        </td>
    </tr>

image.png

举报

相关推荐

0 条评论