Extremetable User Guide: Limit Guide

Source: Internet
Author: User
1. Introduction

When you need to process a large amount of data, you should consider using the limit feature of extremetable. The limit command is from mysql. The limit interface is used to perform limit processing on the result set of a table. The limit implementation knows how users interact with tables when sorting, filtering, paging, and exporting. With this information, you will be able to display the correct filtered and sorted request pages in the most effective way.

To demonstrate the limit feature, the work I will do will be divided into JSP, controller, service, and Dao. This demonstrates a way to use Tiering to handle limit. You can increase or decrease layers as needed. In this example, the Spring framework is used to obtain the data obtained using spring JDBC again. Therefore, yourCodeIt may seem a little different. One feature of extremetable is that it does not depend on any framework or container.

1.1. jsp

To use the limit feature, extremetable must use the specific limit retrieverowscallback, filterrowscallback, and sortrowscallback interfaces. Extremecomponents provides an implementation for each interface, which can be simply used by setting each attribute value to limit.

 
 

Items = "Presidents"
Retrieverowscallback = "Limit"
Filterrowscallback = "Limit"
Sortrowscallback = "Limit"
View = "Limit"
>
...

In addition, for view attributes, refer to a custom view named limit. This is a simple modification of the default extremetable view, excluding the implementation of the toolbar on the last page. This is only related to whether you can obtain the exact required rows. Some databases, such as Oracle and MySQL, provide a feature for row determination, but other databases, such as Sybase, do not. In my example, I think that your database does not support this feature in the worst case.

Even if your database does not provide the ability to get specific rows, limit makes sense when you consider how users work with tables. Users usually sort, filter, and pagination data. In this example, 15 pieces of data constitute one page. The first page requires 15 pieces of data, the second page requires 30 pieces of data, and the third page requires 45 pieces of data, and so on. After a period of paging, they often use filtering to extract data. Even if they do not, they have to paging a large amount of data before that, which affects efficiency. Of course, if you are allowed to click on the last page, all the data will be taken out, which will greatly affect the efficiency.

1.2. Controller

Tip: the controller of the Spring framework is very similar to the action of the Struts framework.

The Controller first needs to create a limit. To accomplish this, you need some help on context and limitfactory.

 
Context context = new httpservletrequestcontext (request );
Limitfactory = new tablelimitfactory (context );
Limit = new tablelimit (limitfactory );

Context is an interface for processing and retrieving attributes. limitfactory uses context to find out how users interact with extremetable. Then limit uses limitfactory to assemble itself.

To initialize limit, it will contain all useful information. This information includes how data is sorted and filtered, which page is displayed, and whether data can be exported.

However, the limit still needs to get the row information so that the correct information page can be displayed to the user. The row information includes the start row, end row, and current displayed row. The controller must obtain the information from the service, and the service will obtain the information from the Dao. Here I only provide the Controller code.

 
Int totalrows = presidentsservice. gettotalpresidents (limit. getfilterset (), limit. isexported ());
Limit. setrowattributes (totalrows, defaultrowsdisplayed );

Limit needs to get all rows to get the row information. Service needs to know which data is filtered, whether or not the data is exported. To set row information, the default number of lines displayed on one page needs to be set. This can be achieved by setting a definite value for the rowsdisplayed attribute of tabletag.

Now we only need to get collection data from services.

 
Collection Presidents = presidentsservice. getpresidents (limit. getfilterset (), limit. getsort (), limit. getrowend ());

Because limit already contains all the information, it is very easy. All that needs to be done is to pass in the information of the filter, sort, and last row. The last thing to do is to send the collections and totalrow information back to JSP so that extremetable can know how to display the information.

Request. setattribute ("Presidents", Presidents );
Request. setattribute ("totalrows", new INTEGER (totalrows ));
1.3. Service

The service needs to interact with Dao to obtain the total number of rows and collection.

1.3.1. Get the total number of rows

The first information required by the Controller is the total number of rows.

 
Public int gettotalpresidents (filterset, Boolean isexported ){
String totalquery = presidentsdao. gettotalpresidentsquery ();
String modtotalquery = filterquery (filterset, totalquery );
Int totalrows = presidentsdao. gettotalpresidents (modtotalquery );
If (isexported & totalrows> maxexportrows ){
Totalrows = maxexportrows;
}
Return totalrows;
}

The service and Dao filter the result set together. It works by adding more and statements after the where statement to modify the query string. To do this, you need to work with limit filterset.

Filterset is an array of filter objects. A filter includes a bean property and the value of this filter. Or, simply put, the rows you want to filter and the values they enter. This makes it very easy to interact. Service only needs to iterate all filtersets and call Dao to splice query statements. (Translator's note: The filtering method is to add and statements after the WHERE clause to change the query statement to filter the data)

 
Private string filterquery (filterset, string query ){
If (! Filterset. isfiltered () | filterset. iscleared ()){
Return query;
}

Filter filters [] = filterset. getfilters ();
For (INT I = 0; I <filters. length; I ++ ){
Filter filter = filters [I];
String property = filter. getproperty ();
String value = filter. getvalue ();
Query = presidentsdao. filterquery (query, property, value );
}

Return query;
}

The query modification includes the filter information and the total number of rows. In some cases, this is enough, but there is still a potential problem when the user exports data. To ensure efficient service, data that exceeds the maximum number of rows cannot be exported.

1.3.2. Get collection

The second information required by the Controller is collection.

Public Collection getpresidents (filterset, sort, int rowend ){
String patientsquery = presidentsdao. getpresidentsquery ();
String modpatientsquery = filterquery (filterset, patientsquery );
Modpatientsquery = sortquery (sort, modpatientsquery );
Modpatientsquery = presidentsdao. limitquery (rowend, modpatientsquery );
Return presidentsdao. getpresidents (modpatientsquery );
}

As before, the service and Dao filter the result set together.

In addition, the order by statement must be extended for the query string to sort the data in the correct way. Sort contains a bean property and sortorder value (positive or backward ). Service only needs to use sort to call Dao.

Private string sortquery (sort, string query ){
If (! Sort. issorted ()){
String defaultsortorder = presidentsdao. getdefaultsortorder ();
If (stringutils. isnotblank (defaultsortorder )){
Return query + defaultsortorder;
}

Return query;
}

String property = sort. getproperty ();
String sortorder = sort. getsortorder ();

Return presidentsdao. sortquery (query, property, sortorder );
}

The last modification required for the query string is to add a special database command to limit the result set to be returned. This is the role of the limitquery () method.

1.4. Dao

Dao is responsible for underlying data for the service.

1.4.1. Define a query string

To really understand Dao, the query string needs to be displayed.

This is the Presidents query string of the obtained data:

Private Final Static string presidentsquery =
"Select" +
"President_id presidentid," +
"First_name firstname," +
"Last_name lastname," +
"Nick_name nickname," +
"Concat (first_name,'', last_name) fullname, "+
"Term," +
"Born," +
"Died," +
"Education," +
"Career," +
"Political_party politicalparty" +
"From Presidents ";

This is the query string for the total number of rows:

 
Private Final Static string totalpresidentsquery =
"Select count (*) from Presidents ";
1.4.2. Filter and sort query strings

The two most interesting methods are filtering and sorting.

The filter looks like this:

Public String filterquery (string query, string property, string value ){
Stringbuffer result = new stringbuffer (query );

If (query. indexof ("where") =-1 ){
Result. append ("where 1 = 1"); // stub where clause so can just append and Clause
}

If (property. Equals ("fullname ")){
Result. append ("and Concat (first_name,'', last_name) like '% "+ value +" % '");
} Else if (property. Equals ("nickname ")){
Result. append ("and nick_name like '%" + value + "% '");
} Else {
Result. append ("and" + property + "like '%" + value + "% '");
}

Return result. tostring ();
}

The correct and statement must be added to the query string for the filterquery () method.

Sort looks very similar:

Public String sortquery (string query, string property, string sortorder ){
Stringbuffer result = new stringbuffer (query + "order ");

If (property. Equals ("fullname ")){
Result. append ("Concat (first_name,'', last_name) "+ sortorder );
} Else {
Result. append (property + "" + sortorder );
}

Return result. tostring ();
}

The sortquery () method requires adding the correct order by statement to the query string.

1.4.3. Limit query string

Now, the query string can be correctly filtered and sort. It also needs to be modified so that only relevant data is displayed on the page. MySQL is the limit command.

 
Public String limitquery (INT rowend, string query ){
Return query + "Limit" + rowend;
}
1.4.4. Retrieve the total number of rows and collection.

The only thing required by the service is the total number of rows and collection.

Public Collection getpresidents (final string query ){
Return jdbctemplate. Query (query, new resultreader (){
List Results = new arraylist ();
Public list getresults (){
Return results;
}

Public void processrow (resultset RS)
Throws sqlexception {
President = new president ();
President. setpresidentid (New INTEGER (Rs. getint ("presidentid ")));
President. setfirstname (Rs. getstring ("firstname "));
President. setlastname (Rs. getstring ("lastname "));
President. setnickname (Rs. getstring ("nickname "));
President. setfullname (Rs. getstring ("fullname "));
President. setterm (Rs. getstring ("term "));
President. setborn (Rs. getdate ("Born "));
President. setdied (Rs. getdate ("died "));
President. seteducation (Rs. getstring ("education "));
President. setcareer (Rs. getstring ("career "));
President. setpoliticalparty (Rs. getstring ("politicalparty "));
Results. Add (President );
}
});
}

Public int gettotalpresidents (final string query ){
Return jdbctemplate. queryforint (query );
}

Resultreader is a special spring class that helps process JDBC queries. It serves as a callback to process JDBC resultset. Jdbctemplate is an abstraction of JDBC connections.

1.4.5. Default sort order

Finally, this is the default sort order required by the Service:

 
Public String getdefaultsortorder (){
Return "order by Concat (first_name,'', last_name )";
}
 
Http://www.blogjava.net/lucky/archive/2006/02/23/32059.html

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.