A simple paging implementation (1)

xiaoxiao2021-03-06  66

Paging problems are very common, developers almost all, and the principle of paging can refer to the previous article: http://blog.9cbs.net/treeroot/archive/2004/09/29/121071.aspx

Many people do not have good enough, often leading to chaos, code readability, but have experience in achieving a JavaBean to achieve paging feature, but this JavaBean is designed and bringing a lot of problems. Some even make the program logic more confusing. Here is recommended: http://blog.9cbs.net/treeroot/articles/122802.aspx

Design this paging function to achieve the target: 1. And implementation, this can be multiplexed. 2. In line with the principle of opening and closing, that is, you can add functions, but you don't need to modify it. 3. It is simple and easy to understand.

The following design takes only one page data each time, and each time you need to reset the total number of queries, how to achieve your own implementation, this is a relatively universal paging implementation. Here is an interface: package treeroot.util; import java.util.list; / *** This interface is used to implement paging function, note that no modification is available here. * @Author Treerot * @version 1.0 * @since 2004-9-30 * / public interface pageable {/ ** * get data results * @return * / public list getResult (); / ** * Get total query * @return * / Public int getCount (); / ** * Get per page number * @return * / public int getpageSize (); / ** * Get the current page number * @return * / public int getCurrentPage (); / ** * Get the total number of pages * @return * / public int getPages (); / ** * Default display record number * / public final static int default_pageSize = 20;} This interface is very simple, including a list of results and some paging If necessary, pay attention to a few points here: 1. The implementation of this interface is a page of data for a certain query, and the last query is not related to the last query 2. The implementation of this interface should be read-only, that is, can't modify of. 3.GetPages () method is redundant, but this method is still provided here.

Abstract implementation of a given below: package treeroot.util; import java.util.List; / *** @author treerot * @version 1.0 * @since 2004-9-30 * / public abstract class AbstractPage implements Pageable {private int currentPage ; private int pageSize; private int pages; protected int count; protected List result; / ** * Indicates the current page * @param currentPage * @throws PageException * / public AbstractPage (int currentPage) {this (currentPage, Pageable.DEFAULT_PAGESIZE); }

/ ** * Indicates the current page and the page size * @param currentPage * @param pageSize * @throws PageException * / public AbstractPage (int currentPage, int pageSize) {this.currentPage = currentPage; this.pageSize = pageSize;} protected void checkPage (int currentpage) THROWS PageException {IF (CurrentPage <1) || (CurrentPage> this.getpages ())) Throw new pageException ("page Over the range: total page number" this.getPages () ", current The page is " currentpage);} / ** * This method is rewritten by subclass to initialize, that is, calculates the count value and the result result, and calls in the constructor of the subclass. * / Abstract protected void init () throws PageException; public List getResult () {return result;} public int getCount () {return count;} public int getPageSize () {return pageSize;} public int getCurrentPage () {return currentPage; PUBLIC INT getpages () {if (pages == 0) this.pages = (count pagesize-1) / pagesize; return pages;}} This abstraction class implements all methods in the interface, but defines an abstract method INIT (), must implement this method in subclasses. One of the above interfaces and an abstract class look simple, you may feel that there is no done, but it doesn't do anything, but you can bring great help to the development. We can inherit this abstraction class according to your needs, and data can be obtained in a variety of ways, such as getting through a list, or through JDBC, Hibernate, etc., but we need to package the results inside, through Hibernate It is particularly convenient. PageException is an exception from the package treeroot.util / *** @author defined treeroot * @version 1.0 * @since 2004-9-30 * / public class PageException extends Exception {public PageException () {super ();} public PageException (String Message) {super (message);}}

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

New Post(0)