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