Mybatis paging plug-in PageHelper

Time:2020-3-28

Mybatis paging plug-in PageHelper

If you are also using mybatis, it is recommended to try the paging plug-in, which must be the most convenient paging plug-in. The paging plug-in supports any complex single table and multi table paging.

Mybatis paging plug-in PageHelper

PageHelper is a paging plug-in of mybatis, which is responsible for paging the written SQL statements

Use of PageHelper

AdvantageYou don’t need to package and care about SQL paging. It’s very convenient to use, and it’s also very convenient to get data from the front end.

1. Introduce PageHelper dependency

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2<ersion>
</dependency>

2. Configure applicationcontext.xml file

In springsqlsessionfactoryOfbeanAdd a page blocker property to

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="plugins">
                <array>
                    <bean class="com.github.pagehelper.PageInterceptor">
                        <property name="properties">
                            <value>
                                <! -- set your database type here -- >
                               helperDialect=mysql
                            </value>
                        </property>
                    </bean>
                </array>
    </property>
</bean>

3. Calling PageHelper

Static method of calling PageHelper in service methodstartPage(attention)Be sure to call this method before actually querying the database.), pass in the page number and size of each page to be queried, and return the pageinfo object provided by the PageHelper plug-in. It can automatically complete the physical paging of the database without adding manually in your SQL statementlimitclause

Mybatis paging plug-in PageHelper

4. Structure of pageinfo

Please refer to the source code for the structure of pageinfo, which is shown here by the returned JSON. Take the corresponding properties of pageinfo object as needed.

Mybatis paging plug-in PageHelper