Using Pagehelper is very simple and requires only:
1. Configure the MyBatis blocker plugin in MyBatis
2. Configure the dialect of the database to determine the database
3. Setting up paging also requires only a static method of calling the class:
Pagehelper.startpage (1, 30);
/*
Configuration file for MyBatis
Sqlmapconfig.xml
*/
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Configuration
Public "-//mybatis.org//dtd Config 3.0//en"
"Http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<!--Configuring the paging plug-in Pagehelper supports multiple databases--
<plugins>
<!--paging interceptors (interceptors provided by MyBatis)--
<plugin interceptor= "Com.github.pagehelper.PageHelper" >
<!--configuration Database dialect---
<property name= "dialect" value= "MySQL"/>
</plugin>
</plugins>
</configuration>
/*
Test
*/
Public class Pagehelpertest {
@Test
public void page () {
//1. Get Mapper Object
@SuppressWarnings ("resource")
ApplicationContext applicationcontext = new Classpathxmlapplicationcontext (
"classpath:spring/ Applicationcontext-*.xml ");
Tbitemmapper itemmapper = Applicationcontext.getbean (Tbitemmapper.class);
//2. Set paging Pagehelper This class is valid only for the most recent query statement, and the second one is invalid
Pagehelper.startpage (1, +);
3. Execute the query
Tbitemexample example = new Tbitemexample ();
list<tbitem> list = Itemmapper.selectbyexample (example);
//4. Get the paging effect
for (Tbitem tbitem:list) {
System.out.println (tbitem);
}
PageInfo contains some information about paging: Total number of records, total pages, current pages, paging size, etc.
pageinfo<tbitem> PageInfo = new pageinfo<> (list);
int pages = Pageinfo.getpages ();
System.out.println ("pages:" + pages);
int pageSize = Pageinfo.getpagesize ();
System.out.println ("pagesizes:" + pageSize);
int size = Pageinfo.getsize ();
SYSTEM.OUT.PRINTLN ("Size:" + size);
Long total = Pageinfo.gettotal ();
System.out.println ("total:" + all);
}
}
The results are as follows:
Tbitem [id=858025, title= Samsung I8552 White Unicom 3G mobile phone dual sim, sellpoint= affordable Machine ~ ~ Spring start Good time ~, price=79900, num=99999, barcode=null, image =http://image.taotao.com/jd/d958a21cec814fdeab934d43b4fb2e06.jpg, cid=560, Status=1, Created=Sun Mar 21:27:49 CST , Updated=sun Mar 21:27:49 CST 2015]
Tbitem [id=860275, title= Changhong (Changhong) 3d51c1080i 51-inch shutter 3D smart Android TV (black), sellpoint= smart Android system free to install applications <a target= " Blank "href=" http://sale.jd.com/act/Kt0aHzbU7uR1M.html ">" Click to enter Changhong New Year "</A>, price=269900, num=99999, Barcode=null, Image=http://image.taotao.com/jd/08dabc37342943ffb717632f9ee40685.jpg, cid=76, Status=1, Created=Sun Mar 21:27:35 CST, Updated=sun Mar 21:27:35 CST 2015]
pages:104
Pagesizes:30
Size:30
total:3096
Pagination Tool Pagehelper Learning (Spring+mybatis)