Use criteria for paging query common method extraction

Source: Internet
Author: User

Using the criteria for query operations, whether it is paging or adding query conditions are very simple, the author preliminary study of the extraction of public methods, in order to facilitate the direct call in the project

Class Commondaoimpl of common methods
Package Org.t31.carvrol.common.impl;

Import java.io.Serializable;
Import java.sql.CallableStatement;
Import java.sql.Connection;
Import java.sql.SQLException;
Import java.util.List;


Import Org.hibernate.Criteria;
Import org.hibernate.HibernateException;
Import Org.hibernate.Query;
Import Org.hibernate.SessionFactory;
Import Org.hibernate.criterion.Order;
Import Org.t31.carvrol.util.PageResult;


public class Commondaoimpl {
Private Sessionfactory sessionfactory;
public void Setsessionfactory (Sessionfactory sessionfactory) {
This.sessionfactory = sessionfactory;
}


/**
* Subclass gets the criteria by this method, sets the query criteria and calls the criteria as arguments
* Public Pageresult Listbypage (criteria criteria, Pageresult Pageresult) method for paging query
* @param clazz
* @return
*/
Public Criteria Getcriteria (Class clazz) {
Return Sessionfactory.getcurrentsession (). Createcriteria (Clazz);
}


/**
* Paging query with query criteria
* @param criteria
* @param pageresult
* @return
*/
Public Pageresult Listbypage (criteria criteria, Pageresult Pageresult) {
if (!pageresult.getorderby (). Equals ("")
&&!pageresult.getorderby (). Equals (null)) {
if ("desc". Comparetoignorecase (Pageresult.getsort ()) = = 0)
Criteria.addorder (Order.desc (Pageresult.getorderby ()));
else {
Criteria.addorder (ORDER.ASC (Pageresult.getorderby ()));
}
}
int counts = Criteria.list (). Size ();
Criteria.setfirstresult (Pageresult.getfirstrec ());
Criteria.setmaxresults (Pageresult.getpagesize ());
Pageresult.setlist (Criteria.list ());
int pageSize = Pageresult.getpagesize ();
int pages = GetPages (counts, pageSize);
Pageresult.setrecords (counts);
Pageresult.setpages (pages);
return pageresult;
}


/*
* Calculate Total Pages
*/
private int GetPages (int counts, int pageSize) {
if (counts = = 0) {
return 0;
else if (counts <= pageSize) {
return 1;
else if (counts%pagesize!=0) {
return counts/pagesize + 1;
} else {
return counts/pagesize;
}
}


}

The implementation class of the DAO layer Customdaoimpl the class where the common method is inherited

Package Org.t31.carvrol.dao.impl;

Import java.io.Serializable;
Import java.util.List;
Import Org.hibernate.Criteria;
Import org.hibernate.criterion.Restrictions;
Import Org.t31.carvrol.common.impl.CommonDaoImpl;
Import Org.t31.carvrol.dao.CustomDao;
Import Org.t31.carvrol.entity.Custom;
Import Org.t31.carvrol.util.PageResult;

public class Customdaoimpl extends Commondaoimpl implements Customdao {

public boolean addcustom (Object o) {
return Super.add (o);
}

public boolean Delcustom (Class clazz, Serializable ID) {
Return Super.del (clazz, id);
}

Public List Listcustom (String hql) {
Return Super.list (HQL);
}

public boolean updatecustom (Object o) {
return Super.update (o);
}

/**
* Paging query with query criteria
*/
Public Pageresult listbypage (Class clazz, Pageresult Pageresult,
Custom Custom) {
Criteria Criteria=super.getcriteria (Clazz);
Add Query criteria
Criteria.add (Restrictions.le ("CID", 7));//less than or equal to 7
Criteria.add (Restrictions.like ("ccustomname", "% sheep%"));

return Super.listbypage (criteria, pageresult);
}

}


Sets the paging parameter to receive the query result class Pageresult
Package org.t31.carvrol.util;

Import java.util.ArrayList;
Import java.util.List;

public class Pageresult<e> {
Public Pageresult () {}

Private list<e> List =new arraylist<e> ();
private int pageno=1;
private int pagesize=10;
private int records = 0;
private int Pages=1;
Private String by = "";
Private String sort = "ASC";

Public list<e> getlist () {
return list;
}
public void setlist (list<e> List) {
This.list = list;
}
public int Getpageno () {
return pageno;
}
public void Setpageno (int pageno) {
This.pageno = PageNo;
}
public int getpagesize () {
Return (pagesize==0) 10:pagesize;
}
public void setpagesize (int pageSize) {
This.pagesize = pageSize;
}
public int getrecords () {
return records;
}
public void Setrecords (int records) {
This.records=records;
}


public int Getfirstrec ()
{
int ret = (This.getpageno ()-1) * this.getpagesize ();
RET = (Ret < 1)? 0:ret;
return ret;
}
Public String Getorderby () {
return by;
}
public int getpages () {
return pages;
}
public void Setorderby (String by) {
This.orderby = by;
}
Public String Getsort () {
return sort;
}
public void Setsort (String sort) {
This.sort = sort;
}
public void setpages (int pages) {
this.pages = pages;
}

}


DAO implementation Class Customdaoimpl, set the parameters of paging query, call the common paging Query method

Package Org.t31.carvrol.dao.impl;

Import java.io.Serializable;
Import java.util.List;
Import Org.hibernate.Criteria;
Import org.hibernate.criterion.Restrictions;
Import Org.t31.carvrol.common.impl.CommonDaoImpl;
Import Org.t31.carvrol.dao.CustomDao;
Import Org.t31.carvrol.entity.Custom;
Import Org.t31.carvrol.util.PageResult;

public class Customdaoimpl extends Commondaoimpl implements Customdao {

/**
* Paging query with query criteria
*/
Public Pageresult listbypage (Class clazz, Pageresult Pageresult,
Custom Custom) {
Pageresult pr=new Pageresult ();
Page size
Pr.setpagesize (2);
Current page
Pr.setpageno (1);
Based on the CID reverse
Pr.setorderby ("CID");
Pr.setsort ("desc");

Invoke the parent class method to get the criteria
Criteria Criteria=super.getcriteria (Clazz);
Add Query criteria
Criteria.add (Restrictions.le ("CID", 7));//less than or equal to 7
Criteria.add (Restrictions.like ("ccustomname", "% sheep%"));

return Super.listbypage (criteria, pageresult);
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.