【MyBatis】第十篇:mybatis的分页插件---pageHelper

文章介绍了MyBatis的PageHelper分页插件的使用方法,包括在pom.xml和mybatis-config.xml中的配置,以及如何通过PageHelper.startPage()开启分页查询。示例展示了如何通过PageHelper进行分页查询并获取Page对象和PageInfo对象,包含了如页码、每页数量、总页数等信息,便于在前端展示分页效果。
摘要由CSDN通过智能技术生成

分页无论是那个开发都是常见的使用场景,当然对于分页需要如果自己写的,不过自己写的话可能会需要想到很多:

比如:通过查询sql判断有多少数据,在页面显示共有多少页面。然后每页返回的数据是多少,上一页以及下一页如果到第一页和最后一页后是否起效等等。

这个就有一个mybatis的分页插件pageHelper,其本质就是mybatis拦截器的一个应用,实现分页查询。

如果需要深入了解可以看一下:官网

还是老规矩进行演示吧,文字解说不如直接用例子演示:

第一步

这个需要两步,分布需要配置依赖和插件。

这个配置在pox.xml配置文件中

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<!-- 配置依赖环境 --> 
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.2.0</version>
</dependency>

这个需要配置在mybatis-config.xml.毕竟这个是mybatis的适配插件,所以需要配置在mybatis的核心配置文件中。

<!-- 设置分页的插件 --> 
<plugin interceptor="com.github.pagehelper.PageInterceptor">
 
 </plugin>

第二步

体验插件工具其它的配置就不再多说直接使用逆向工程的配置环境,不重写搭建了 不然篇幅又变得很长而琐碎了。传送阵

首先使用一个非分页的查询吗,可以查询出所有的数据:

 @Test
    public void test(){
        try {
            //         通过IO操作配置文件  所以说这个配置文件可以改名字,一般默认是mybatis-config.xml
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//        SqlSession类似与jdb中connection,但是其通过SqlSession工厂而创建的,而这个工厂却是通过工厂的构造类SqlSessionFactoryBuilder得到工厂
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//        返回SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//        得到SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        这个通过代理模式,传入什么类返回什么类
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

            StudentExample studentExample=new StudentExample();
            List<Student> studentList= studentMapper.selectByExample(studentExample);
            studentList.forEach(System.err::println);
            sqlSession.commit();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

在这里插入图片描述

这个时候没有启动分页插件,其实插件的使用有点像是javaweb中的过滤器一样,在请求数据的时候会先通过插件所带的方法。如下写:

  @Test
    public void test(){
        try {
            //         通过IO操作配置文件  所以说这个配置文件可以改名字,一般默认是mybatis-config.xml
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//        SqlSession类似与jdb中connection,但是其通过SqlSession工厂而创建的,而这个工厂却是通过工厂的构造类SqlSessionFactoryBuilder得到工厂
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//        返回SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//        得到SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        这个通过代理模式,传入什么类返回什么类
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            StudentExample studentExample=new StudentExample();
//            这里开启分页
            PageHelper.startPage(1,2);
            List<Student> studentList= studentMapper.selectByExample(studentExample);
            studentList.forEach(System.err::println);
            sqlSession.commit();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

在这里插入图片描述

可见直接这样写,返回的数据数据第一页,分页数据为2条。

然后打印出数据:

            Page<Object> page = PageHelper.startPage(1,2);
            List<Student> studentList= studentMapper.selectByExample(studentExample);
            studentList.forEach(System.err::println);
//            得到所有的班级数据
            System.err.println(page);

看一下page内容是:

Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=6, pages=3, reasonable=false, pageSizeZero=false}[Student [Hash = 540325452, studentId=1, studentName=张三, studentAge=18, studentSex=男, gradeId=2, serialVersionUID=1], Student [Hash = 1976804832, studentId=2, studentName=赛貂蝉, studentAge=14, studentSex=女, gradeId=1, serialVersionUID=1]]

可见有具体的页数,以及总共有多少条数据,以及返回的当前页面的数据。

当然还有更加具体数据的对象PageInfo:

 @Test
    public void test(){
        try {
            //         通过IO操作配置文件  所以说这个配置文件可以改名字,一般默认是mybatis-config.xml
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//        SqlSession类似与jdb中connection,但是其通过SqlSession工厂而创建的,而这个工厂却是通过工厂的构造类SqlSessionFactoryBuilder得到工厂
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//        返回SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//        得到SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        这个通过代理模式,传入什么类返回什么类
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            StudentExample studentExample=new StudentExample();
//            这里开启分页
            Page<Object> page = PageHelper.startPage(1,2);
            List<Student> studentList= studentMapper.selectByExample(studentExample);
            studentList.forEach(System.err::println);
//             在页面显示有多少也的参数 navigateLastPage
            PageInfo<Student> pageInfo= new PageInfo<>(studentList,4);
//            得到所有的班级数据
            System.err.println(pageInfo);
//            还可以添加条件

            sqlSession.commit();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

其PageInfo输出的如下

在这里插入图片描述

因为PageInfo有点不详细,调整其格式如下看:

在这里插入图片描述

现在说一下具体的意义:

  • pageNum:当前页的页码
  • pageSize:每页显示的条数
  • startRow / endRow : 这个是数据库返回的数据在本页是第几条到第几条,比如例子是第一页所以开始1,结束2.
  • size:当前页面显示的真实条数
  • total:总记录数
  • pages: 总页数
  • prePage / nextPage:上一页页码 / 下一页页码
  • isFirstPage / isLastPage : 是否是第一页 / 是否是最后一页
  • navigatePages : 设置的导航页数字 比如例子中是4 是通过 new PageInfo<>(studentList,4); 得到的
  • navigateFirstPage / navigateLastPage : 导航页第一页 / 最后一页 这里可以看出设置页数和真实数据还是有差异的
  • navigatepageNums :导航分页的页码

这些可以传递到页面,然后页面根据这些数据进行取出判断即可。

MyBatis PageHelper 是一个 MyBatis 分页插件,能够快速、便捷的进行分页查询,支持多种数据库。使用 PageHelper 可以避免手写 SQL 语句进行分页操作,同时 PageHelper 支持物理分页和逻辑分页两种方式。 下面是使用 PageHelper 进行分页查询的步骤: 1. 导入 PageHelper 依赖 Maven 项目在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> ``` 2. 配置 PageHelperMyBatis 的配置文件中添加以下配置: ``` <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="dialect" value="mysql"/> </plugin> </plugins> ``` 其中 dialect 属性指定了数据库类型,PageHelper 支持的数据库类型包括:oracle、mysql、mariadb、sqlite、hsqldb、postgresql、db2、sqlserver、informix、达梦、人大金仓、南大通用、神通、PostgreSQL9.3-9.5。 3. 使用 PageHelper 进行分页查询 在需要进行分页查询的方法中使用 PageHelper.startPage 方法进行分页设置,然后调用查询方法获取查询结果。例如: ``` PageHelper.startPage(1, 10); // 第一页,每页显示 10 条记录 List<User> userList = userDao.selectUserList(); // 查询用户列表 PageInfo<User> pageInfo = new PageInfo<>(userList); // 封装分页结果 ``` 其中 PageHelper.startPage 方法接收两个参数,第一个参数为当前页码,第二个参数为每页显示的记录数。 最后使用 PageInfo 类对查询结果进行封装,得到分页结果。PageInfo 类中包含了分页信息和查询结果。 以上就是使用 MyBatis PageHelper 进行分页查询的基本步骤。需要注意的是,在使用 PageHelper 进行分页查询时,需要确保查询语句中不要使用 limit 关键字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值