SSH2+Daoz项目中的分页查询

Page.java

Java代码  收藏代码
  1. import java.util.List;  
  2. /** 
  3.  * 分页Page类 
  4.  * @author zhxing 
  5.  * 
  6.  * @param <T> 
  7.  */  
  8. public class Page<T>{  
  9.     public final int DEFAULT_PAGESIZE= 10;   // 每页记录数   
  10.     private List<T> result = null;//页面数据  
  11.       
  12.     private int totalRows;//总记录数  
  13.     private int pageSize=DEFAULT_PAGESIZE;//每页显示行数  
  14.     private int currentPage;//当前页数  
  15.     private int totalPages;//总页数  
  16.     private int startRow;//查询开始的记录数  
  17.       
  18.       
  19.     public Page(){  
  20.           
  21.     }  
  22.     public Page(int totalRows){  
  23.         //初始化总记录数  
  24.         this.totalRows=totalRows;  
  25.         //初始化总页数  
  26.         this.totalPages=totalRows/pageSize;  
  27.         if(totalRows%pageSize!=0){  
  28.             totalPages++;  
  29.         }  
  30.         //初始化当前页数  
  31.         this.currentPage=1;  
  32.         //初始化查询开始的记录数  
  33.         this.startRow=0;          
  34.     }  
  35.   
  36.   
  37.     // 页内的数据列表.  
  38.   
  39.     public List<T> getResult() {  
  40.         return result;  
  41.     }  
  42.   
  43.     public void setResult(List<T> result) {  
  44.         this.result = result;  
  45.     }  
  46.   
  47.      // 获得上一页  
  48.   
  49.     public void previous(){  
  50.         if(currentPage==1)  
  51.             return;  
  52.         currentPage--;  
  53.         startRow=(currentPage-1)*pageSize;  
  54.     }  
  55.     //获得下一页  
  56.     public void next(){  
  57.         if(currentPage==totalPages){  
  58.             return;  
  59.         }  
  60.         currentPage++;  
  61.         startRow=(currentPage+1)*pageSize;  
  62.     }  
  63.     public int getCurrentPage() {  
  64.         return currentPage;  
  65.     }  
  66.     //设置当前页  
  67.     public void setCurrentPage(int currentPage) {  
  68.   
  69.         if(currentPage>=totalPages){  
  70.             if(totalPages==0)  
  71.                 this.currentPage=1;  
  72.             else  
  73.                 this.currentPage=totalPages;  
  74.         }  
  75.         if(1<currentPage&&currentPage<totalPages){  
  76.             this.currentPage=currentPage;  
  77.         }  
  78.         if(currentPage<1){  
  79.             this.currentPage=1;  
  80.         }  
  81.               
  82.     }     
  83.     public int getStartRow() {  
  84.         startRow=(currentPage-1)*pageSize;  
  85.         return startRow;  
  86.     }  
  87.     public void setStartRow(int startRow) {  
  88.         this.startRow = startRow;  
  89.     }  
  90.     public int getTotalRows() {  
  91.         return totalRows;  
  92.     }  
  93.     public void setTotalRows(int totalRows) {  
  94.         this.totalRows = totalRows;  
  95.     }  
  96.     public int getTotalPages() {  
  97.         return totalPages;  
  98.     }  
  99.     public int getPageSize() {  
  100.         return pageSize;  
  101.     }  
  102.     public boolean isStart() {  
  103.         return currentPage==1;  
  104.     }  
  105.     public boolean isEnd() {  
  106.         return currentPage==totalPages||totalPages==0;  
  107.     }  
  108.   
  109. }  
import java.util.List;
/**
 * 分页Page类
 * @author zhxing
 *
 * @param <T>
 */
public class Page<T>{
	public final int DEFAULT_PAGESIZE= 10;   // 每页记录数 
	private List<T> result = null;//页面数据
	
	private int totalRows;//总记录数
	private int pageSize=DEFAULT_PAGESIZE;//每页显示行数
	private int currentPage;//当前页数
	private int totalPages;//总页数
	private int startRow;//查询开始的记录数
	
	
	public Page(){
		
	}
	public Page(int totalRows){
		//初始化总记录数
		this.totalRows=totalRows;
		//初始化总页数
		this.totalPages=totalRows/pageSize;
		if(totalRows%pageSize!=0){
			totalPages++;
		}
		//初始化当前页数
		this.currentPage=1;
		//初始化查询开始的记录数
		this.startRow=0;		
	}


	// 页内的数据列表.

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	 // 获得上一页

	public void previous(){
		if(currentPage==1)
			return;
		currentPage--;
		startRow=(currentPage-1)*pageSize;
	}
    //获得下一页
	public void next(){
		if(currentPage==totalPages){
			return;
		}
		currentPage++;
		startRow=(currentPage+1)*pageSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	//设置当前页
	public void setCurrentPage(int currentPage) {

		if(currentPage>=totalPages){
			if(totalPages==0)
				this.currentPage=1;
			else
				this.currentPage=totalPages;
		}
		if(1<currentPage&&currentPage<totalPages){
			this.currentPage=currentPage;
		}
		if(currentPage<1){
			this.currentPage=1;
		}
			
	}	
	public int getStartRow() {
		startRow=(currentPage-1)*pageSize;
		return startRow;
	}
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public int getPageSize() {
		return pageSize;
	}
	public boolean isStart() {
		return currentPage==1;
	}
	public boolean isEnd() {
		return currentPage==totalPages||totalPages==0;
	}

}

 BaseDao.java

Java代码  收藏代码
  1. import java.io.Serializable;  
  2. import java.util.List;  
  3.   
  4. public interface BaseDao<T,ID extends Serializable> {  
  5.     /** 
  6.      * 保存实体 
  7.      * @param entity 实体类 
  8.      */  
  9.     public void save(T entity);  
  10.     /** 
  11.      * 删除实体 
  12.      * @param entity 实体类 
  13.      */  
  14.     public void delete(T entity);  
  15.     /** 
  16.      * 根据实体id 删除实体 
  17.      * @param entityClass  实体类 
  18.      * @param id  实体id 
  19.      */  
  20.     public void deleteById(Class<T> entityClass,ID id);  
  21.     /** 
  22.      * 更新实体 
  23.      * @param entity  实体类 
  24.      */  
  25.     public void update(T entity);  
  26.     /** 
  27.      * 根据实体id 查询单个实体 
  28.      * @param entityClass 实体类 
  29.      * @param id 实体id 
  30.      * @return 
  31.      */  
  32.     public T findById(Class<T> entityClass,ID id);  
  33.     /** 
  34.      * 列出所有实体集合 
  35.      * @param entityClass 实体类 
  36.      * @return 实体类List 
  37.      */  
  38.     public List<T> findAll(Class<T> entityClass);  
  39.     /** 
  40.      * 根据实体参数,查询符合条件的实体类集合 
  41.      * @param hql  
  42.      * @param values 参数 
  43.      * @return 
  44.      */  
  45.     public List<Object> find(String hql, Object... values);  
  46.   
  47.     /** 
  48.      * 根据hql 语句,返回Page 类 
  49.      * @param page Page类 
  50.      * @param hql @param hql 
  51.                  * @param currentPage 当前页码 
  52.      * @return 
  53. */  
  54.     public Page<T> findByPage(final String hql,final String countHql,final int currentPage);  
  55.       
  56. }  
import java.io.Serializable;
import java.util.List;

public interface BaseDao<T,ID extends Serializable> {
	/**
	 * 保存实体
	 * @param entity 实体类
	 */
	public void save(T entity);
	/**
	 * 删除实体
	 * @param entity 实体类
	 */
	public void delete(T entity);
	/**
	 * 根据实体id 删除实体
	 * @param entityClass  实体类
	 * @param id  实体id
	 */
	public void deleteById(Class<T> entityClass,ID id);
	/**
	 * 更新实体
	 * @param entity  实体类
	 */
	public void update(T entity);
	/**
	 * 根据实体id 查询单个实体
	 * @param entityClass 实体类
	 * @param id 实体id
	 * @return
	 */
	public T findById(Class<T> entityClass,ID id);
	/**
	 * 列出所有实体集合
	 * @param entityClass 实体类
	 * @return 实体类List
	 */
	public List<T> findAll(Class<T> entityClass);
	/**
	 * 根据实体参数,查询符合条件的实体类集合
	 * @param hql 
	 * @param values 参数
	 * @return
	 */
	public List<Object> find(String hql, Object... values);

	/**
	 * 根据hql 语句,返回Page 类
	 * @param page Page类
	 * @param hql @param hql
                 * @param currentPage 当前页码
	 * @return
*/
	public Page<T> findByPage(final String hql,final String countHql,final int currentPage);
	
}

BaseHibernateDao.java

Java代码  收藏代码
    1. import java.io.Serializable;  
    2. import java.sql.SQLException;  
    3. import java.util.List;  
    4.   
    5. import org.hibernate.HibernateException;  
    6. import org.hibernate.Query;  
    7. import org.hibernate.Session;  
    8. import org.springframework.orm.hibernate3.HibernateCallback;  
    9. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
    10.   
    11. public class BaseHibernateDao<T,ID extends Serializable> extends HibernateDaoSupport implements BaseDao<T,ID> {  
    12.   
    13.     @Override  
    14.     public void delete(T entity) {  
    15.         this.getHibernateTemplate().delete(entity);  
    16.           
    17.     }  
    18.   
    19.     @Override  
    20.     public void deleteById(Class<T> entityClass, ID id) {  
    21.         delete(this.findById(entityClass, id));  
    22.           
    23.     }  
    24.   
    25.     @Override  
    26.     public T findById(Class<T> entityClass, ID id) {  
    27.         return (T)this.getHibernateTemplate().get(entityClass, id);  
    28.     }  
    29.   
    30.     @Override  
    31.     public List<T> findAll(Class<T> entityClass) {  
    32.         String name=entityClass.getName();  
    33.         return this.getHibernateTemplate().find("from"+name);  
    34.     }  
    35.   
    36.     @Override  
    37.     public void save(T entity) {  
    38.         this.getHibernateTemplate().save(entity);  
    39.     }  
    40.   
    41.     @Override  
    42.     public void update(T entity) {  
    43.         this.getHibernateTemplate().update(entity);  
    44.     }  
    45.   
    46.     @Override  
    47.     public List<Object> find(String hql, Object... values) {  
    48.         return this.getHibernateTemplate().find(hql,values);  
    49.     }  
    50.   
    51.     @Override  
    52.     public Page<T> findByPage(final String hql,final String countHql,final int currentPage) {  
    53.         // TODO Auto-generated method stub  
    54.         return (Page<T>)this.getHibernateTemplate().execute(new HibernateCallback(){  
    55.             @Override  
    56.             public Object doInHibernate(Session session)  
    57.                     throws HibernateException, SQLException {  
    58.                 //初始化Page  
    59.                 Page<T> page=new Page<T>(getCount(session, countHql));  
    60.                 page.setCurrentPage(currentPage);  
    61.                                            //分页查询开始  
    62.                 Query query=session.createQuery(hql);  
    63.                 query.setFirstResult(page.getStartRow());  
    64.                 query.setMaxResults(page.getPageSize());  
    65.                 //把取得的实体list设置到Page 类中  
    66.                 page.setResult(query.list());  
    67.                 return page;  
    68.             }  
    69.             //获取总记录数  
    70.             private int getCount(Session session,String countHql){  
    71.                 Long count=0L;  
    72.                 List list=session.createQuery(countHql).list();  
    73.                 if(list!=null&&list.size()==1)  
    74.                     count=(Long)list.get(0);  
    75.                 return count.intValue();  
    76.             }  
    77.         });  
    78.     }  
    79.   
    80.   

原文地址:https://www.cnblogs.com/sailormoon/p/2827024.html