MyBatis-06- notes

MyBatis paging plugin

Paging plugin

1 Introduction to Paging Plugin

Insert picture description here
  • Paging can display many results in pages.
  • If the current page is on the first page, there is no previous page. If the current page is on the last page, there is no next page.
  • It is necessary to clarify the current page and how many results are displayed on this page.
  • MyBatis paging plugin summary
  1. In enterprise-level development, paging is also a common technique. The MyBatis currently used does not have a paging function. If we want to realize the paging function, we need to manually write the LIMIT statement (MySQL database dialect). However, different databases implement different SQL statements for paging, so the cost of handwriting paging is higher. At this time, you can use the paging plug-in to help us realize the paging function.
  2. PageHelper: Third-party paging assistant. Encapsulate complex paging operations, so that the paging function becomes very simple.

2 Use of Paging Plugin

MyBatis can use third-party plug-ins to extend the functions. The paging assistant PageHelper encapsulates the complex operations of paging, and you can get the relevant data of the paging in a simple way.

Development steps:

①Import the jar package with PageHelper (pagehelper depends on things

Insert picture description here


in jsqlparser ) ②Configure the PageHelper plug-in in the mybatis core configuration file

<!-- 注意:分页助手的插件  配置在通用mapper之前 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
    <!-- 指定方言 -->
    <property name="dialect" value="mysql"/>
</plugin>

The modified core configuration file is as follows

<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<!--configuration 核心根标签-->
<configuration>

    <!--引入数据库连接的配置文件-->
    <properties resource="jdbc.properties"/>

    <!--配置LOG4J-->
    <settings>
        <setting name="logImpl" value="log4j"/>
    </settings>

    <!--起别名-->
    <typeAliases>
        <typeAlias type="com.itheima.bean.Student" alias="student"/>
        <!--<package name="com.itheima.bean"/>-->
    </typeAliases>

    <!--集成分页助手插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

    <!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个-->
    <environments default="mysql">
        <!--environment配置数据库环境  id属性唯一标识-->
        <environment id="mysql">
            <!-- transactionManager事务管理。  type属性,采用JDBC默认的事务-->
            <transactionManager type="JDBC"></transactionManager>
            <!-- dataSource数据源信息   type属性 连接池-->
            <dataSource type="POOLED">
                <!-- property获取数据库连接的配置信息 -->
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>

    <!-- mappers引入映射配置文件 -->
    <mappers>
        <!-- mapper 引入指定的映射配置文件   resource属性指定映射配置文件的名称 -->
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>

③The paging function is realized through the paging assistant PageHelper

package com.itheima.paging;

public class Test01 {
    @Test
    public void selectPaging() throws Exception{
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        //2.获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //3.通过工厂对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //4.获取StudentMapper接口的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过分页助手来实现分页功能 //参数1:当前页 参数2:每页显示条目
        // 第一页:显示3条数据
        //PageHelper.startPage(1,3); 
        // 第二页:显示3条数据
        //PageHelper.startPage(2,3);
        // 第三页:显示3条数据
        PageHelper.startPage(3,3);

        //5.调用实现类的方法,接收结果
        List<Student> list = mapper.selectAll();//查询全部数据

        //6.处理结果
        for (Student student : list) {
            System.out.println(student);
        }

        //获取分页相关参数
        PageInfo<Student> info = new PageInfo<>(list); //构造函数入参是分页查询结果
        System.out.println("总条数:" + info.getTotal());
        System.out.println("总页数:" + info.getPages());
        System.out.println("当前页:" + info.getPageNum());
        System.out.println("每页显示条数:" + info.getPageSize());
        System.out.println("上一页:" + info.getPrePage());
        System.out.println("下一页:" + info.getNextPage());
        System.out.println("是否是第一页:" + info.isIsFirstPage());
        System.out.println("是否是最后一页:" + info.isIsLastPage());

        //7.释放资源
        sqlSession.close();
        is.close();
    }
}

3 Get the parameters of the paging plug-in

Get other parameters related to paging :

  • PageInfo: A functional class that encapsulates paging related parameters
//其他分页的数据
PageInfo<User> pageInfo = new PageInfo<User>(select);//构造函数入参是分页查询结果
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页:"+pageInfo.getPageNum());
System.out.println("每页显示长度:"+pageInfo.getPageSize());
System.out.println("是否第一页:"+pageInfo.isIsFirstPage());
System.out.println("是否最后一页:"+pageInfo.isIsLastPage());

4 Summary of Paging Plugin Knowledge

​ Paging: Many results can be displayed in pages.

  • Paging plugin jar package: pagehelper-5.1.10.jar jsqlparser-3.1.jar
 <plugins>:集成插件标签。 
  • Paging assistant related API
  - PageHelper:分页助手功能类。
  	- startPage():设置分页参数 
  - PageInfo:分页相关参数功能类。 
  	- getTotal():获取总条数 
	- getPages():获取总页数
	- getPageNum():获取当前页
	- getPageSize():获取每页显示条数
	- getPrePage():获取上一页 
	- getNextPage():获取下一页 
  	- isIsFirstPage():获取是否是第一页 
	- isIsLastPage():获取是否是最后一页