mybatis的分页插件pageHelper

来源:互联网 发布:中国统一台湾 知乎 编辑:程序博客网 时间:2024/04/29 09:44

https://github.com/pagehelper/Mybatis-PageHelper/tree/master/src/main/java/com/github/pagehelper


实现原理:




使用方法:
1、引入jar包
2、

需要在SqlMapConfig.xml中配置插件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 配置分页插件 --><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->                <property name="dialect" value="mysql"/></plugin></plugins></configuration>

3、在查询的sql语句执行之前,添加一行代码:

PageHelper.startPage(1, 10);

第一个参数是page,要显示第几页。

第二个参数是rows,没页显示的记录数。

4、取查询结果的总数量。

创建一个PageInfo类的对象,从对象中取分页信息。

public class TestPageHelper {@Testpublic void testPageHelper(){//1、创建一个Spring容器ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");//2、从Spring容器中获取mapper对象TbItemMapper bean = applicationContext.getBean(TbItemMapper.class);//3、执行sql并分页TbItemExample example=new TbItemExample();//4、分页处理PageHelper.startPage(1, 6);List<TbItem> selectByExample = bean.selectByExample(example);System.out.println(selectByExample.size());for (TbItem tbItem : selectByExample) {System.out.println(tbItem.getTitle());}PageInfo<TbItem> pages=new PageInfo<>(selectByExample);long total = pages.getTotal();System.out.println("总记录数============"+total);}}




0 0
原创粉丝点击