MyBatis page plug-in Pagehelper

Source: Internet
Author: User
Tags postgresql sqlite
1. Description

If you are also using MyBatis, it is recommended to try the page plug-in, this must be the most convenient use of the paging plugin.

The plugin currently supports the oracle,mysql,mariadb,sqlite,hsqldb,postgresql of six database pages.

2. How to use

First step: Configure the Interceptor plugin in the MyBatis configuration xml:

<plugins>
    <!--com.github.pagehelper is Pagehelper class package name--
    <plugin interceptor= " Com.github.pagehelper.PageHelper ">
        <!--set Database type Oracle,mysql,mariadb,sqlite,hsqldb,postgresql Six kinds of databases-- >       
        <property name= "dialect" value= "MySQL"/>
    </plugin>
</plugins>

Step two: Use in code

1. Set Paging information:

Get 1th page, 10 content, default query total Count
pagehelper.startpage (1);
 The first select method that follows is paged
list<country> List = countrymapper.selectif (1);

2. Take paging information

After paging, the actual returned result of the list type is PAGE<E>, if you want to remove the paging information, you need to cast to Page<e>,
page<country> listcountry = (page< country>) List;
Listcountry.gettotal ();

3, the second method of taking paging information

Get 1th page, 10 content, default query total Count
pagehelper.startpage (1);
list<country> list = Countrymapper.selectall ();
Wrapping the results with PageInfo
PageInfo page = new PageInfo (list);
The test PageInfo All properties
//pageinfo contains a very comprehensive paging property
assertequals (1, Page.getpagenum ());
Assertequals (Page.getpagesize ());
Assertequals (1, Page.getstartrow ());
Assertequals (Page.getendrow ());
Assertequals (183, Page.gettotal ());
Assertequals (Page.getpages ());
Assertequals (1, Page.getfirstpage ());
Assertequals (8, Page.getlastpage ());
Assertequals (True, Page.isfirstpage ());
Assertequals (False, Page.islastpage ());
Assertequals (False, Page.ishaspreviouspage ());
Assertequals (True, Page.ishasnextpage ());

3.TestPageHelper

@Test public
void Testpagehelper () {
    //Create a spring container
    applicationcontext applicationcontext = new Classpathxmlapplicationcontext ("Classpath:spring/applicationcontext-*.xml");
    Obtains the proxy object of mapper from the spring container
    tbitemmapper mapper = Applicationcontext.getbean (tbitemmapper.class);
    Execute query, and paging
    tbitemexample example = new Tbitemexample ();
    Pagination processing
    pagehelper.startpage (2, ten);
    list<tbitem> list = Mapper.selectbyexample (example);
    Take a list of items for
    (Tbitem tbitem:list) {
        System.out.println (Tbitem.gettitle ());
    }
    Take paging information
    pageinfo<tbitem> PageInfo = new pageinfo<> (list);
    Long total = Pageinfo.gettotal ();
    SYSTEM.OUT.PRINTLN ("Total goods:" + All);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.