With Java Reflection, a dynamic combination of dynamic combination of a single table is achieved in Hibernate.

xiaoxiao2021-03-06  22

Reflection is one of the features of the Java program development language, which allows the program to be accessed, and can directly operate the internal properties of the program.

Hibernate is an object / relational database mapping tool for Java environments, basically, each table or view can correspond to a class in Hibernate, where we use this class to implement dynamic combined queries.

First let's take a look at the source code of this function:

/ ** * Combination Query * @Param Object Contains the object of query conditions * @Param FirstResult The first return location (starting from 0 * @Param maxResults Maximum return number * @Param ORDERFIELD Sort field * @param isabs is sequence alignment * @return * @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 = currentations (); criteria criteria = session.createcriteria (Object.getClass ()); for (int i = 0; i

} If (Name.indexof ("get")! = 0 || Name.indexof ("getClass") == 0) // If it is not required, you jump out of 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, jump out of if (ReturnType.Indexof ("String")! = -1) {if ("" If "" ", you jump out of the string field; Criteria.add (expression.like (FieldName, "%" RETOBJ "%")); // For String fields, use the LIKE blurry query} else criteria.add (Expression.Like (FieldName, Retobj));} } If (isasc) criteria.addorder (Order.asc (ORDERFIELD)); // Ascending else criteria.addorder (Order.Desc (ORDERFIELD)); // Descending CRIT Eria.setFirstResult (firstresult); criteria.setMaxResults (MaxResults); list = criteria.list ();} finally {closesession ();} Return List;

Assume that there is a physical table in the relational database, the structure is as follows:

STAFF (Employee Table)

Column Type NULL Description ID (PK) VARCHAR2 (20) Not Null employee number Name VARCHAR2 (20) Not Null employee Name DEPT INTEGER NULL Employee Departure Department (P3) Password varchar2 (20) Not null Password Post Integer Null Employee position (P14) Priv varchar2 (40) Not NULL Rights Birthdayvarchar2 (20) Null Birthday Active Varchar2 (1) Not Null Y: Activation N: Not activated

With tools, generate its associated Hibernate class STAFF, including the following fields: private java.lang.string ID; private java.lang.string name; private java.lang.long dept; private java.lang.String Password; private Java.lang.long post; private java.lang.string priv; private java.lang.string birthday; private java.lang.string active

Below, we can use the QueryList above to query.

Example 1:

The front desk needs a list that contains all personnel.

Then the relevant code is as follows:

Staff staff = new staff ();

List list = querylist (staff, 0,1000, id, true);

Example 2:

Query all the names of "Ding" in all names

Then the relevant code is as follows:

Staff staff = new staff ();

Staff.setname = "D";

List list = querylist (staff, 0,1000, id, true);

Example 3:

Query all the names contain "Ding", and in the activation state

Then the relevant code is as follows:

Staff staff = new staff ();

Staff.setname = "D";

Staff.setActive = "y";

List list = querylist (staff, 0,1000, id, true);

Example 4:

Query all the names contain "Ding", and in the activation state, and the return required to sort by birthday

Then the relevant code is as follows:

Staff staff = new staff ();

Staff.setname = "D";

Staff.setActive = "y";

List list = querylist (staff, 0,1000, birthday, true,);

For the STAFF, you can achieve greater, less than function. For example, we want to implement people after the query is born in 1981-09-16, then you can add the following code in the STAFF class:

Private java.lang.string minbirdhday;

Private java.lang.string maxbirdhday;

Public java.lang.string getMinbirdHDay () {

Return minbirdday;

}

Public void setminbirdhday (java.lang.string minbirdhday) {

THIS.MINBIRTHDAY = MinbirtHDAY;

}

Public java.lang.string getmaxbirthday () {

Return Maxbirthday;

}

Public void setmaxbirdhday (java.lang.string maxbirdhday) {

THIS.MAXBIRTHDAY = MAXBIRTHDAY;

}

Example 5:

Inquiry Birthday After 1978, the activation status personnel before 1981, requiring names to contain "Ding", arrangement according to the birthday order

Staff staff = new staff ();

Staff.setname = "D";

Staff.setActive = "y";

STAFF. SETMINBIRTHDAY ("1978"); staff. setmaxbirdhday ("1981");

List list = querylist (staff, 0,1000, birthday, false);

At this point, it should be said that the introduction is almost the same, maybe someone wants to ask, "Single table, can you do, then what to do?" It is very simple, it is possible to create a view in the database. I hope this experience is useful for everyone.

转载请注明原文地址:https://www.9cbs.com/read-39970.html

New Post(0)