Final code optimization of the javaEE paging function (2)
The paging code was optimized last time, but the disadvantage is that the hql statement is written in the Action layer. If the hql statement is complex, such as the conditional query, some parameter judgments will be made, it is very error-prone. Further Optimization of the next page query will become very simple.
Next we will complete a conditional query. For example, you can select a department, query by name, and query by account.
I. Let's take a look at the simple one. We can further extract the jsp interface code.
(1) The code displayed on the public page has been extracted before, but each interface still needs to write a javascript code to specify the corresponding xxx. action.
In fact, you can also directly define a form,
Then you can get the form and submit the form. Therefore, you can directly write a form tag and specify the action.
We need to complete the condition query, place the content in the form tag, and submit the form to get the corresponding attribute value at the Action layer.
II. The following describes how to write hql statements at the Action layer. You can define a QueryHelper class to splice hql statements and pass parameter lists in the future, after processing, pass it as a parameter to PageBean for processing. (For more information about PageBean, see the previous blog)
(1) You need to splice the from (mandatory statements), where, and orderby statements to define these three attributes and obtain the corresponding parameter list.
(2) define the constructor. when an object is new at the Action layer, the from statement parameter (that is, the class name) is passed)
(3) When splicing a where statement, the Action layer must pass the conditions and parameter list. Note that the parameters here are variable parameters because they are uncertain. For details, see the following annotations.
The whereClause and orderByClause attributes are initialized as null strings. Therefore, the first call of this method is from xxx where xxx. yyy = ?, The following call is where xxx. yyy =? And xxx. zzz =?
In addition, the methods are of the QueryHelper type. return this; indicates the object of the returned class. In this way, you can call your own method to add conditions, you do not need to write one statement separately (For details, refer to the calls in the Action layer below)
(3) The main function of the following method is to determine whether to splice the where statement. The reason for this is that there are multiple interface conditions and users may not select all of them, then you need to determine in the Action, if (xxx) Call stitching; so you can directly call it all the way, without the if judgment in the Action
(4) The same is true for orderby, which is not listed here. This condition query does not need to be sorted.
How to obtain the hql statement:
(5) obtain the hql statement for querying the total number of records and the statement for the parameter list:
III:
(1) Declare the properties passed through the interface in the Action to implement the get and set methods.
(2) construct a QueryHelp object, for example: (you can call your own method through ),
To get the PageBean object, pass the QueryHelp object as a parameter.
(3) further optimization can be performed here, because each Action requires the service to call the getPageBean () method, and then place the resulting PageBean object to the top of the stack. Therefore, you can extract it to the QueryHepler class and write a method to complete this function.
For example:
So here we can get the paging information and place it to the top of the stack. Complete.
Iv. Summary
At this point, code optimization related to paging is almost the same, and subsequent paging writing operations will be very easy and error-prone.