Hibernate3 provides detachedcriteria, which enables us to construct detachedcriteria at the web layer, and then call the bean at the business layer for dynamic condition query. Based on this function, I have designed a general abstract bean base class and paging class support. The Code comes from the corresponding class of the javaeye-core package of quake Wang, and I have made many modifications.
Paging Support class:
Package com. javaeye. Common. util;
Import java. util. List;
Public class paginationsupport {
Public final static int pagesize = 30; // defines the number of rows displayed on a page
Private int pagesize = pagesize;
Private list items; // query result set
Private int totalcount; // The total number of suitable records
Private int [] indexes = new int [0]; // record the start number of each page after you find the total number of records
Private int startindex = 0; // current page number
/* ------------------------------ Three constructors */
Public paginationsupport (list items, int totalcount ){
Setpagesize (pagesize );
Settotalcount (totalcount );
Setitems (items );
Setstartindex (0 );
}
Public paginationsupport (list items, int totalcount, int startindex ){
Setpagesize (pagesize );
Settotalcount (totalcount );
Setitems (items );
Setstartindex (startindex );
}
Public paginationsupport (list items, int totalcount, int pagesize, int startindex ){
Setpagesize (pagesize );
Settotalcount (totalcount );
Setitems (items );
Setstartindex (startindex );
}
Public list getitems (){
Return items;
}
Public void setitems (list items ){
This. Items = items;
}
Public int getpagesize (){
Return pagesize;
}
Public void setpagesize (INT pagesize ){
This. pagesize = pagesize;
}
Public int gettotalcount (){
Return totalcount;
}
// Set the total number of records, total number of pages (count), and start number of each page
Public void setTotalCount (int totalCount ){
If (totalCount> 0 ){
This. totalCount = totalCount;
Int count = totalCount/pageSize;
If (totalCount % pageSize> 0)
Count ++;
Indexes = new int [count];
For (int I = 0; I <count; I ++ ){
Indexes [I] = pageSize * I;
}
} Else {
This. totalCount = 0;
}
}
Public int [] getIndexes (){
Return indexes;
}
Public void setIndexes (int [] indexes ){
This. indexes = indexes;
}
Public int getStartIndex (){
Return startIndex;
}
// Set the start page location
Public void setStartIndex (int startIndex ){
If (totalCount <= 0)
This. startIndex = 0;
Else if (startIndex> = totalCount)
This. startIndex = indexes [indexes. length-1];
Else if (startIndex <0)
This. startIndex = 0;
Else {
This. startIndex = indexes [startIndex/pageSize];
}
}
// Next page
Public int getNextIndex (){
Int nextIndex = getStartIndex () + pageSize;
If (nextIndex> = totalCount)
Return getStartIndex ();
Else
Return nextIndex;
}
// Previous Page
Public int getpreviusindex (){
Int previusindex = getStartIndex ()-pageSize;
If (previusindex <0)
Return 0;
Else
Return previusindex;
}
}
Abstract business class (our class must inherit this abstract class to use the class in it for classification)
Package com. javaeye. Common. business;
Import java. Io. serializable;
Import java. util. List;
Import org. hibernate. criteria;
Import org. hibernate. hibernateexception;
Import org. hibernate. Session;
Import org. hibernate. criterion. detachedcriteria;
Import org. hibernate. criterion. projections;
Import org. springframework. Orm. hibernate3.hibernatecallback;
Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;
Import com. javaeye. common. util. PaginationSupport;
Public abstract class AbstractManager extends HibernateDaoSupport {
Private boolean cacheQueries = false;
Private String queryCacheRegion;
Public void setCacheQueries (boolean cacheQueries ){
This. cacheQueries = cacheQueries;
}
Public void setQueryCacheRegion (String queryCacheRegion ){
This. queryCacheRegion = queryCacheRegion;
}
// Save
Public void save (final object entity ){
Gethibernatetemplate (). Save (entity );
}
Public void persist (final object entity ){
Gethibernatetemplate (). Save (entity );
}
// Update
Public void Update (final object entity ){
Gethibernatetemplate (). Update (entity );
}
// Delete
Public void delete (final Object entity ){
GetHibernateTemplate (). delete (entity );
}
// Load
Public Object load (final Class entity, final Serializable id ){
Return getHibernateTemplate (). load (entity, id );
}
Public Object get (final Class entity, final Serializable id ){
Return getHibernateTemplate (). get (entity, id );
}
// Query all
Public List findAll (final Class entity ){
Return getHibernateTemplate (). find ("from" + entity. getName ());
}
// Query by name
Public List findByNamedQuery (final String namedQuery ){
Return getHibernateTemplate (). findByNamedQuery (namedQuery );
}
// Dynamically query by Parameters
Public List findByNamedQuery (final String query, final Object parameter ){
Return getHibernateTemplate (). findByNamedQuery (query, parameter );
}
Public List findByNamedQuery (final String query, final Object [] parameters ){
Return getHibernateTemplate (). findByNamedQuery (query, parameters );
}
Public List find (final String query ){
Return getHibernateTemplate (). find (query );
}
Public List find (final String query, final Object parameter ){
Return getHibernateTemplate (). find (query, parameter );
}
// The following is a paging Query
Public PaginationSupport findPageByCriteria (final DetachedCriteria detachedCriteria ){
// Use detachedCriteri to display the number of records on one page. The current page is the number of queries (0)
Return findPageByCriteria (detachedCriteria, PaginationSupport. PAGESIZE, 0 );
}
Public PaginationSupport findPageByCriteria (final DetachedCriteria detachedCriteria, final int startIndex ){
// The page number specified by the user
Return findPageByCriteria (detachedCriteria, PaginationSupport. PAGESIZE, startIndex );
}
Public PaginationSupport findPageByCriteria (final DetachedCriteria detachedCriteria, final int pageSize,
Final int startIndex ){
// Specify the number of records displayed on each page and the number of records displayed on the current page.
Return (PaginationSupport) gethibernatetemplate(cmd.exe cute (new HibernateCallback (){
Public Object doInHibernate (Session session) throws HibernateException {
Criteria criteria = detachedCriteria. getExecutableCriteria (session );
Int totalCount = (Integer) criteria. setProjection (Projections. rowCount (). uniqueResult (). intValue ();
Criteria. setProjection (null );
List items = criteria. setFirstResult (startIndex). setMaxResults (pageSize). list ();
PaginationSupport ps = new PaginationSupport (items, totalCount, pageSize, startIndex );
Return ps;
}
}, True );
}
Public List findAllByCriteria (final DetachedCriteria detachedCriteria ){
Return (List) gethibernatetemplate(cmd.exe cute (new HibernateCallback (){
Public Object doInHibernate (Session session) throws HibernateException {
Criteria criteria = detachedCriteria. getExecutableCriteria (session );
Return criteria. list ();
}
}, True );
}
Public int getCountByCriteria (final DetachedCriteria detachedCriteria ){
Integer count = (Integer) gethibernatetemplate(cmd.exe cute (new HibernateCallback (){
Public Object doInHibernate (Session session) throws HibernateException {
Criteria = detachedcriteria. getexecutablecriteria (session );
Return criteria. setprojection (projections. rowcount (). uniqueresult ();
}
}, True );
Return count. intvalue ();
}
}
On the web layer, the user constructs the query condition detachedcriteria and the optional startindex, calls the corresponding findbycriteria method of the Business bean, and returns a paginationsupport instance ps.
PS. getitems () to obtain the paginated result set
PS. getindexes () to obtain the array of the paging Index
PS. gettotalcount ()
Ps. getStartIndex () Current paging Index
Ps. getNextIndex () Next Index
Ps. getpreviusindex () Previous Index