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"/>

The modified core configuration file is as follows

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-// Config 3.0//EN" "">

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

    <properties resource=""/>

        <setting name="logImpl" value="log4j"/>

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

        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>

    <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}" />

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

③The paging function is realized through the paging assistant PageHelper

package com.itheima.paging;

public class Test01 {
    public void selectPaging() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

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

        List<Student> list = mapper.selectAll();//查询全部数据

        for (Student student : list) {

        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());


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);//构造函数入参是分页查询结果

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
  • Paging assistant related API
  - PageHelper:分页助手功能类。
  	- startPage():设置分页参数 
  - PageInfo:分页相关参数功能类。 
  	- getTotal():获取总条数 
	- getPages():获取总页数
	- getPageNum():获取当前页
	- getPageSize():获取每页显示条数
	- getPrePage():获取上一页 
	- getNextPage():获取下一页 
  	- isIsFirstPage():获取是否是第一页 
	- isIsLastPage():获取是否是最后一页