Dynamic | view | combination Query
Reflection is one of the characteristics of Java program development language, it allows the running program to access itself, and can directly manipulate the internal properties of the program.
Hibernate is a Java environment-oriented object/relational database mapping tool, basically, each table or view in the hibernate can correspond to a class, where we make full use of this class, to implement dynamic combinatorial query.
First, let's take a look at the source code for this function:
/** * Combined Query * @param object contains query criteria * @param firstresult the first return position (starting from 0) * @param maxresults Maximum number of returns & nbsp * @param orderfield sorted fields * @param isabs are in a positive order * @return & nbsp; * @throws hibernateexception */ public List queryList ( Object object, int firstresult, int maxresults,string OrderField, Boolean isasc) throws Hibernateexception, Illegalargumentexception,illegalaccessexception,invocationtargetexception { List list=null; Class c = object.getclass (); method method[] = C.getmethods (); try { Session session = CurrentsessIon (); criteria = Session.createcriteria (Object.getclass ()); for (int i = 0; i < method.length i++) { & nbsp; String name = Method[i].getname (); if (Name.indexof ("Getmin") ==0) { //greater than String fieldName = Name.substring (6, 7). toLowerCase () + name.substring (7); Object retobj = Method[i].invoke (object, NULL); if (Retobj!= NULL&&!retobj.equals ("")) Criteria.add (expression.ge (FieldName, retobj)); continue; } if (Name.indexof (" Getmax ") ==0) {//less than String fieldName = name.substring (6, 7). toLowerCase () + name.substring (7); Object retobj = Method[i].invoke (object, NULL); if (retobj!= null &&!retobj.equals ("")) & nbsp; Criteria.add ( Expression.le (FieldName, retobj)); continue; } if (Name.indexof ("get")!= 0 | | | name.indexof ("getclass") = = 0) //If not required method, jump out continue ; String fieldName = Name.substring (3, 4). toLowerCase () + name.substring (4); String returntype = Method[i].getreturntype (). toString (); Object retobj = Method[i].invoke (object, NULL); if (retobj!= null) { //If NULL, no value assigned, jump out if (Returntype.indexof ("String")!=-1) { if (Retobj.equals (")) //If the string field is" ", jump out of continue; Criteria.add (Expression.like (fieldName, "%" + retobj + "%")); For string fields, use the like Blur query }else Criteria.add (Expression.like (FieldName, retobj)); } } if (ISASC) Criteria.addorder (ORDER.ASC (OrderField)); //Ascending else Criteria.addorder (Order.desc (OrderField)); //Descending Criteria.setfirstresult (Firstresult); criteria.setmaxresults (maxresults); list = Criteria.list (); } finally { closesession (); }
return list; }
Suppose the relational database has a physical table with the following structure:
STAFF (Employee table)
Column Name
Type
Null
Description
Id (PK)
VARCHAR2 (20)
Not NULL
Employee work number
Name
VARCHAR2 (20)
Not NULL
Employee Name
Dept
INTEGER
Null
Department of employee (P3)
Password
VARCHAR2 (20)
Not NULL
Password
Post
INTEGER
Null
Position of employee (P14)
Priv
VARCHAR2 (40)
Not NULL
Permission Word
Birthday
VARCHAR2 (20)
Null
Birthday
Active
VARCHAR2 (1)
Not NULL
Y: Activating N: Inactive
Use the tool to generate its associated Hibernate class staff, which includes the following fields:
Below, we can use the above querylist to carry on the query
Example 1:
The front desk needs a list of all the people,
So the relevant code is as follows:
Staff staff=new Staff ();
List list=querylist (staff,0,1000,id,true);
Example 2:
Find people with "ding" in all their names
So the relevant code is as follows:
Staff staff=new Staff ();
Staff.setname= "Ding";
List list=querylist (staff,0,1000,id,true);
Example 3:
Query for all names that contain "ding" and who are in the active state
So the relevant code is as follows:
Staff staff=new Staff ();
Staff.setname= "Ding";
Staff.setactive= "Y";
List list=querylist (staff,0,1000,id,true);
Example 4:
Query all people whose names contain "ding" and are active, and ask the returned list to be sorted by birthday
So the relevant code is as follows:
Staff staff=new Staff ();
Staff.setname= "Ding";
Staff.setactive= "Y";
List list=querylist (Staff,0,1000,birthday,true,);
To staff processing, you can achieve greater than, less than the function, such as we want to implement query after 1981-09-16 people, then you can add the following code in the Staff class:
Private java.lang.String Minbirthday;
Private java.lang.String Maxbirthday;
Public java.lang.String Getminbirthday () {
return minbirthday;
}
public void Setminbirthday (java.lang.String minbirthday) {
This.minbirthday = Minbirthday;
}
Public java.lang.String Getmaxbirthday () {
return maxbirthday;
}
public void Setmaxbirthday (java.lang.String maxbirthday) {
This.maxbirthday = Maxbirthday;
}
Example 5:
Query birthday After 1978 years, before 1981, the activation status of personnel, required name contains "Ding", in descending order of birthdays
Staff staff=new Staff ();
Staff.setname= "Ding";
Staff.setactive= "Y";
Staff. Setminbirthday ("1978");
Staff. Setmaxbirthday ("1981");
List list=querylist (Staff,0,1000,birthday,false);
So far, it should be said that the introduction of almost, perhaps someone to ask, "a single table query, you can do so, then the table query how to do?" "In fact, it is very simple to create a view within the database." I hope this experience will be useful to you.
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.