Use of Page Helper in MyBatis

Use of Page Helper in MyBatis

A brief introduction to PageHelper

PageHelper is an excellent open source mybatis paging plug-in in China. It supports basic mainstream and common databases, such as mysql,oracle and so on.
Its project address on github: pageHelper-MyBatis

Simple use of PageHelper

Step 1: Import dependencies in pom.xml

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

Step 2: Configure the interceptor plug-in in Spring's configuration file

 <!--To configure SqlSessionFactory factory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--To configure pagehelper-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">oracle</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

Step 3: Use

Call PageHelper.startPage static method before you need to paginate MyBatis query method. The first MyBatis query method that follows this method will be paginated.

 @Override
    public  List<Order> findAll(int page, int size) throws Exception {

        //The parameter pageNum is the page number value, and the parameter pageSize represents the number of bars displayed per page.
        PageHelper.startPage(page,size);
        List<Order> orderList=orderDao.findAll(page,size);
        return orderList;
    }

Tags: Mybatis Spring github xml

Posted on Wed, 09 Oct 2019 19:09:45 -0700 by dvt85