0
回答
Hibernate用hql实现分页查询
滴滴云服务器,限时包月0.9元,为开发者而生>>>   
看了一个项目,里面用到hibernate,对数据库分页查询时用的是Criteria和自定义的一个封装类Page,求大神分析一下下面分页查询的方法什么意思,俺知识浅薄,实在看不懂。。。。。。谢谢!!

Page类如下:



/**
 * 封装分页和排序参数及分页查询结果.
 */
public class Page<T> {


public static int DEFAULT_PAGESIZE = 20;


public static int DEFAULT_PAGE = 1;


private int pageNo = DEFAULT_PAGE;


private int pageSize = DEFAULT_PAGESIZE;

private static int PAGE_LENGTH = 5;


private int totalCount = -1;


//用户请求的路径,翻页时还是当前页面
String requestPath = "";

private String pagerStr;

private String pagerStrEN;

private int startPage=1;

private int endPage=1;

private List<T> list = null;


public Page() {
  
}
public Page(int pageSize) {
this.pageSize = pageSize;
}
public Page(int pageNo, int pageSize) {
this.pageNo = pageNo;
this.pageSize = pageSize;
}


public Page(int pageNo, List<T> list, int pageSize, int totalCount) {
this.pageNo = pageNo;
this.list = list;
this.pageSize = pageSize;
this.totalCount = totalCount;
}


/**
* 第一条记录在结果集中的位置,序号从0开始.
*/
public int getStart() {
if (pageNo < 0 || pageSize < 0)
return -1;
else
return ((pageNo - 1) * pageSize);
}


/**
* 总页数.
*/
public int getTotalPageCount() {
int count = totalCount / pageSize;
if (totalCount % pageSize > 0) {
count++;
}
return count;
}


/**
* 是否还有下一页.
*/
public boolean isHasNextPage() {
return (pageNo + 1 <= getTotalPageCount());
}


/**
* 返回下页的页号,序号从1开始.
*/
public int getNextPage() {
if (isHasNextPage())
return pageNo + 1;
else
return pageNo;
}


/**
* 是否还有上一页. 
*/
public boolean isHasPrePage() {
return (pageNo - 1 >= 1);
}


/**
* 返回上页的页号,序号从1开始.
*/
public int getPrePage() {
if (isHasPrePage())
return pageNo - 1;
else
return pageNo;
}


/**
* 每页的记录数量.
*/
public int getPageSize() {
return pageSize;
}


public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}


/**
* 当前页的页号,序号从1开始.
*/
public int getPageNo() {
return pageNo;
}


public void setPageNo(int page) {
this.pageNo = page;
}


/**
* 页内的数据列表.
*/
public List<T> getList() {
return list;
}


public void setList(List<T> list) {
this.list = list;
}


/**
* 总记录数量.
*/
public int getTotalCount() {
return totalCount;
}


public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/**
     * 获取当前记录数
     * @return 
     */
    public int getCurrentRecord() {
            if(pageNo==0)
                    return 0;
            
            return pageSize*(pageNo-1);
    }
    
    public void setRequestPath(String requestPath) {
this.requestPath = requestPath;
}


public int getStartPage() {
if(this.pageNo-PAGE_LENGTH>0)
 startPage=this.pageNo-PAGE_LENGTH;
return startPage;
}


public int getEndPage() {
if(this.pageNo+PAGE_LENGTH<=this.getTotalPageCount())
 endPage=this.pageNo+PAGE_LENGTH;
else endPage = this.getTotalPageCount();
return endPage;
}

public String getRequestPath() {
return requestPath;
}
    
    //中文版分页
    public String getPagerStr()
{
StringBuffer buf=new StringBuffer();
if(requestPath.indexOf("?")<0)
requestPath += "?";
Pattern p=Pattern.compile("&{0,1}pager.pageNo=\\d{1,5}"); 
Matcher m=p.matcher(requestPath); 
String requestPath=m.replaceAll("");
if(this.getTotalPageCount()>0){
buf.append("<div class='div_btn2'>");
buf.append("共"+String.valueOf(this.getTotalPageCount())+"页");
}
if(this.getTotalPageCount()>1)
{
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo=1\" class=\"btn_3\">首页</a>&nbsp;");
if(this.getPageNo()>1)
{
buf.append("<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getPageNo()-1)+"\" class=\"btn_3\" >上一页</a>");
}
if(this.getPageNo()<=1)
{
buf.append("<span class=\"btn_2\">上一页</span>");
}
for(int n=this.getStartPage();n<=getEndPage();n++){
if(n==this.pageNo){
buf.append("&nbsp;<span class=\"btn_current\">"+n+"</span>&nbsp;");
}
else{
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo="+n+"\" rel=\"next\" class=\"no_link\">"+n+"</a>&nbsp;");
}
}
if(this.getPageNo()<this.getTotalPageCount())
{
buf.append("<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getPageNo()+1)+"\" class=\"btn_3\">下一页</a>");
}
if(this.getPageNo()>=this.getTotalPageCount())
{
buf.append("<span class=\"btn_2\">下一页</span>");
}
buf.append("<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getTotalPageCount())+"\" class=\"btn_3\">尾页</a>");

}
if(this.getTotalPageCount()>0)
buf.append("</div>\n");
 
return buf.toString();
 
}


//英文版分页
public String getPagerStrEN() {
StringBuffer buf=new StringBuffer();
if(requestPath.indexOf("?")<0)
requestPath += "?";
Pattern p=Pattern.compile("&{0,1}pager.pageNo=\\d{1,3}"); 
Matcher m=p.matcher(requestPath); 
String requestPath=m.replaceAll("");
if(this.getTotalPageCount()>0){
buf.append("<div class=\"page_tyil\">");
buf.append(String.valueOf(this.getTotalPageCount())+"&nbsp;<font style='font-size:13px;'>Page(s)</font>");
}
if(this.getTotalPageCount()>1)
{
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo=1\" class=\"btn_3\" rel=\"First\">First</a>&nbsp;");
if(this.getPageNo()>1)
{
buf.append("<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getPageNo()-1)+"\" class=\"btn_3\" rel=\"next\">Previous</a>");
}
if(this.getPageNo()<=1)
{
buf.append("<span class=\"btn_2\">Previous</span>");
}
for(int n=this.getStartPage();n<=getEndPage();n++){
if(n==this.pageNo){
buf.append("&nbsp;<span class=\"btn_current\">"+n+"</span>&nbsp;");
}
else{
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo="+n+"\" rel=\"next\">"+n+"</a>&nbsp;");
}
}
if(this.getPageNo()<this.getTotalPageCount())
{
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getPageNo()+1)+"\" class=\"btn_3\" rel=\"next\">Next</a>&nbsp;");
}
if(this.getPageNo()>=this.getTotalPageCount())
{
buf.append("<span class=\"btn_2\">Next</span>");
}
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getTotalPageCount())+"\" class=\"btn_3\" rel=\"next\">Last</a>&nbsp;");
            
}
 
if(this.getTotalPageCount()>0)
buf.append("</div>\n");
/*测试
System.out.println("----------------------");
System.out.println(buf.toString());
System.out.println("----------------------");*/
return buf.toString();
}




//英文版分页
public String getPagerStrEN1() {

StringBuffer buf=new StringBuffer();
if(requestPath.indexOf("?")<0)
requestPath += "?";
Pattern p=Pattern.compile("&{0,1}pager.pageNo=\\d{1,3}"); 
Matcher m=p.matcher(requestPath); 
String requestPath=m.replaceAll("");
if(this.getTotalPageCount()>0){
buf.append("<div class=\"eu_bws_y\">");
}
if(this.getTotalPageCount()>1)
{
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo=1\"><<</a> &nbsp;");
if(this.getPageNo()>1)
{
buf.append("<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getPageNo()-1)+"\"><</a>");
}
if(this.getPageNo()<=1)
{
buf.append("<");
}


buf.append(" Page " + this.pageNo + " of " + String.valueOf(this.getTotalPageCount()) + " ");
 
if(this.getPageNo()<this.getTotalPageCount())
{
buf.append("&nbsp;<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getPageNo()+1)+"\">></a>");
}
if(this.getPageNo()>=this.getTotalPageCount())
{
buf.append(">");
}

buf.append(" &nbsp;<a href=\""+requestPath+"&pager.pageNo="+String.valueOf(this.getTotalPageCount())+"\">>></a>&nbsp;");
         }
 
if(this.getTotalPageCount()>0)
buf.append("</div>\n");
 
return buf.toString();
}
}

------------------------------------------------------------------------------------

分页查询方法(在另一个类中)如下,此处Criteria c为条件查询后的结果作为参数传进来的

/**
* 对Criteria进行分页相关操作,包括查出总记录数,设定分页参数
*/
public void pageCriteria(Criteria c, Page page) {
CriteriaImpl impl = (CriteriaImpl) c;
// 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
Projection projection = impl.getProjection();
List<CriteriaImpl.OrderEntry> orderEntries;
try {
orderEntries = (List) BeanUtils.forceGetProperty(impl, "orderEntries");
System.out.println(orderEntries.size());
BeanUtils.forceSetProperty(impl, "orderEntries", new ArrayList());
} catch (Exception e) {
throw new InternalError(" Runtime Exception impossibility throw ");
}
// 执行查询
int totalCount = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();
System.out.println(totalCount);
if (totalCount < 1){
page.setTotalCount(totalCount);
return;
}
System.out.println(projection);
// 将之前的Projection和OrderBy条件重新设回去
c.setProjection(projection);
if (projection == null) {
c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}

try {
BeanUtils.forceSetProperty(impl, "orderEntries", orderEntries);
} catch (Exception e) {
throw new InternalError(" Runtime Exception impossibility throw ");
}


page.setTotalCount(totalCount);


c.setFirstResult(page.getStart()).setMaxResults(page.getPageSize());
}

<无标签>
举报
struts_hibe...
发帖于4年前 0回/4K+阅
顶部