When querying data, people often need to set query conditions. In SQL or HQL statements, query conditions are often placed in the WHERE clause. In addition, hibernate supports the criteria query, which encapsulates the query criteria as a criteria object. In practical applications, the Createcriteria () method of the session is used to construct a Org.hibernate.Criteria instance, and then the specific query conditions are added to the criteria instance through the Add () method of the criteria. In this way, programmers can query data without using SQL or even hql.
Let's look at a simple example:
Criteria cr = Session.createcriteria (Student.class); Generate a Criteria Object
Cr.add (Restrictionseq ("name", "XY")); Equivalent to where name= ' xy '
List List = Cr.list ();
Let's look at the operator such a comparison.
hql operator |
QBC operator |
meaning |
= |
Restrictions.eq () |
equals equal |
<> |
Restrictions.ne () |
Not equal to not equal |
> |
RESTRICTIONS.GT () |
Greater than greater than |
>= |
Restrictions.ge () |
Greater than or equal to greater than or equal |
< |
Restrictions.lt () |
Less than less than |
<= |
Restrictions.le () |
Less than or equal to less than or equal |
is null |
Restrictions.isnull () |
equals null value |
is not null |
Restrictions.isnotnull () |
Non-null value |
like |
Restrictions.like () |
String pattern Matching |
and |
Restrictions.and () |
Logic and |
and |
Restrictions.conjunction () |
Logic and |
or |
Restrictions.or () |
Logical OR |
or |
Restrictions.disjunction () |
Logical OR |
not |
Restrictions.not () |
Logical non |
In (list) |
Restrictions.in () |
equals one of the values in the list |
Not in (list) |
Restrictions.not (restrictions.in ()) |
is not equal to any one of the values in the list |
between x and y |
Restrictions.between () |
Any value in XY of closed interval |
Not between x and y |
Restrictions.not (restrictions. Between ()) |
Less than value x or greater than value Y |
When will the criteria be the most powerful? Is the time for multiple conditional queries.
The view is this:
School Number <input type= "text" name= "Dto.num"/>
Name <input type= "text" name= "Dto.name"/>
<input type= "Submit" value= "Query"/>
we can do this:
Entity Layer
public class Student
{
private int id;
private String name;
Private Date birth;
...........
}
DTO Layer
public class Studentdto
{
private int id; Student number
Private String name;//Student name
public int getId ()
{
return ID;
}
public void setId (int id)
{
This.id = ID;
}
Public String GetName ()
{
return name;
}
public void SetName (String name)
{
THIS.name = name;
}
}
Impl Layer
Public Pagemodel Getqqueryresult (studentdto dto, int start, int pagesize)
{
Criteria C = this.getsession (). Createcriteria (Student.class);
if (0!= dto.getid ())
{
C.add (Restrictions.like ("id", Dto.getid ());
}
if (null!= dto.getname () &&! "". Equals (Dto.getname ()))
{
C.add (Restrictions.like ("XM", "%" + dto.getname () + "%"));
}
int count = C.list (). Size ();
List datas = C.setfirstresult (start). Setmaxresults (pagesize). List ();
int totalpage = Helper.totalpage (count, pagesize);
Get the result set
Pagemodel pm = new Pagemodel ();
Pm.settotal (count);
Pm.setdatas (datas);
Pm.settotalpage (Totalpage);
return PM;
}
....... Here is the knowledge of struts and SSH paging, and there is no more to repeat. ^ ^
Reference Address: http://blog.csdn.net/dengqf/article/details/6603531