The so-called paging, which is divided from the database, encapsulates a paging class. Pagination is done using paging objects.
However, paging often has query criteria.
three important data for the paging class : "Current page number", "total number of records in database", "number of data displayed per page"
principle : SELECT * from "table name" where "field name" like "'% condition% '" limit "Start Query index", "data displayed per page"
Two-step paging with query criteria
(1) Step one: Find out the total number of data that meets the criteria
---->select COUNT (*) from "table name" where "field name" like "condition"
(2) Step two: Provide three important data, raw Page object, and then query the data of the specified page number in the database
---->select * from "table name" where "field name" like "'% condition% '" limit "Start Query index", "data displayed per page"
Paging under Hibernate framework "paged core source with query criteria"
(1) method of obtaining the total number of records for the data that meets the criteria
/**
* Gets the number of records in the database with query criteria
* @Title: Countbyname
* @Description: TODO (here is a word describing the effect of this method)
* @param seachname Query conditions
* @return
* @return Integer Returns the total number of records for qualifying data
* @author Shang
* @date 2014-7-1 9:22:14
*/
Public Integer countbyname (final String seachname) {
Return Super.gethibernatetemplate (). Execute (new hibernatecallback<integer> () {
Public Integer Doinhibernate (session session)
Throws Hibernateexception, SQLException {
Query query=session.createsqlquery ("SELECT count (*) from Sys_user where Sys_user_name like?");
Query.setparameter (0, "%" +seachname+ "%");
Number number= (Java.lang.Number) Query.uniqueresult ();
return Number.intvalue ();
}
});
}
(2) The total number of records according to the qualifying data, the current number of pages provided by the front desk, the number of data bars displayed per page, the raw Page object
Gets the paging object (the current number of pages, the total number of records for qualifying data, and the number of data bars displayed per page)
This.page=new page (num, countrecords, pagerecords);
(3) According to the paging object, query conditions, using the Hibernate framework for paging (can be paged with SQL statements, here with HQL paging, also hibernate paging)
/**
*
* @Title: Querybyqueryname
* @Description: TODO (here is a word describing the effect of this method)
* @param parameters of queryname query condition
* @param Firstresult starting from the first few data (provided by the paging object)
* @param the number of data bars displayed per page (provided by the paging object) Maxresult
* @return
* @return List<sysuser> Returns the data collection for the specified page number
* @author Shang
* @date 2014-7-1 5:02:07
*/
@SuppressWarnings ("Unchecked")
Public list<sysuser> querybyqueryname (final String queryname,final integer firstresult,final integer maxresult) {
Return (list<sysuser>) super.gethibernatetemplate (). Executefind (New Hibernatecallback<list<sysuser >> () {
@Override
Public List<sysuser> Doinhibernate (session session)
Throws Hibernateexception, SQLException {
Query query=session.createquery ("from Sysuser where Sysusername like?"); /paging HQL statement with query criteria
Query.setparameter (0, "%" +queryname+ "%");
Query.setfirstresult (Firstresult);//start querying from the first few data
Query.setmaxresults (Maxresult);//How many data are displayed per page
return Query.list ();
}
});
}
(4) Pagination category "for reference only, omit Set,get method, code is simple, thinking important"
public class page{
Shows the number of data bars per page
private int pageSize;
Current Page Count
private int pagenum;
Total number of records in database
private int rowCount;
Total pages
private int PageCount;
How many records to start the query
private int rowstart;
Do you have a previous page
Private Boolean hasprevious=false;
Previous page
private int previouspage;
Home
private int firstpage;
Do you have the next page?
Private Boolean hasnext=false;
Next page
private int nextPage;
End Page
private int lastpage;
Number of pages displayed per page
private int everypagecount=10;
Number of pages to start per page
private int everypagestart;
Number of pages to end per page
private int everypageend;
Public Page () {}
Public Page (String pageSize, string pagenum, int rowCount) {
This.pagesize=pagesize==null?10:integer.parseint (pageSize);
This.pagenum=pagenum==null?1:integer.parseint (Pagenum);
This.rowcount=rowcount;
Total pages
this.pagecount= (int) Math.ceil (this.rowcount*1.0/this.pagesize);
When the last page of data is deleted, it will cause pagenum>pagecount, so assign a value.
if (This.pagenum>this.pagecount) {
This.pagenum=this.pagecount;
}
Every time you start with the first few records, select * from Onesong limit Rowstart, pageSize
This.rowcount= (this.pagenum-1) *this.pagesize;
When the number of pages is greater than 1, there is a previous page, and the home
if (this.pagenum>1) {
This.hasprevious=true;
This.previouspage=this.pagenum-1;
this.firstpage=1;
}
When the number of pages is less than 1 there is the next page, and the last
if (This.pagenum<this.pagecount) {
This.hasnext=true;
this.nextpage=this.pagenum+1;
This.lastpage=this.pagecount;
}
The start and end of the number of pages displayed per page
This.everypagestart= (THIS.PAGENUM-THIS.PAGESIZE/2) <=0?1: (THIS.PAGENUM-THIS.PAGESIZE/2);
This.everypageend= (this.everypagestart+this.everypagecount-1) >=this.pagecount?pagecount: ( THIS.EVERYPAGESTART+THIS.EVERYPAGECOUNT-1);
}
}