Java Data Paging General encapsulation __java

Source: Internet
Author: User
Principle

Database paging, from the customer's point of view, is mainly to provide two parameters: the number of per page (pageSize), the current page (currentpage). From the background processing, the main is the paging query database, the total number of queries, so just take care of these two parameters can be completed pagination. The content of this article

This article's encapsulation reference Easyjweb pagelist name, (only used before, inheritance relationship implementation should be different).

A total of two core interfaces have been designed in this article:

Ipagelist: Data query and result-hosted main interface

Ipager: page-related processing interface

In addition, the provision of abstract classes, do public processing, see Abstractpagelist, Abstractpager. If you want to implement using this interface because of the persistence layer using technology, inherit the Abstractpagelist class and implement the abstract method. To implement the paging component of your own page display, inherit the Abstractpager class and implement the abstract method.

This article, in the download below, will have the implementation of the two ipagelist implementations of Ibatis and MyBatis. This means that if your project uses both of these persistence layers, you can use them directly. Otherwise, inherit abstractpagelist code that implements the response persistence layer. How to use
Ipagelist plist=new xxxpagelist ([param]);//Initialize, set parameter Plist.dolist ();//Issue actual query Plist.getresult ();//optional operation, get and process the corresponding data Ipager pager=new Xxxpager (pagelist);//create Pagination Component Class Pager.save2request (Request, "listname");//Save data, and Page component to page page placement in the appropriate location ${ Pager

If this is not very clear, I give a web three applications in the actual code, easy to understand (Struts2+spring+mybatis) action layer

Public String dolist () {
	httpservletrequest request=servletactioncontext.getrequest ();
	String callnum=request.getparameter ("UserName");
	String accnum=request.getparameter ("phone");
		
	if (Stringutils.isblank (userName) &&stringutils.isblank (phone)) {
		Request.setattribute ("message", " Query parameters cannot be full empty ");
		return "list";
	Gets the paging parameter
	String currentpage=request.getparameter ("CurrentPage");
	String pagesize=request.getparameter ("pageSize");
		
	Hashmap<string,string> param=new hashmap<string,string> ();
	Param.put ("UserName", userName);
	Param.put ("Phone", phone);
	Param.put ("CurrentPage", currentpage);
	Param.put ("PageSize", pageSize);
		
	ipagelist<userinfo> plist=service.selectlist (param);
	Ipager pager=new Minipager (plist);
	Pager.save2request (Request, "List");
		
	return "list";
Service layer, only transparently passed to DAO DAO layer
Public ipagelist<userinfo> selectlist (HashMap param) {ipagelist

You should be able to understand this more easily. On the page, just add ${pager to the list below the table, which contains the JS, and the page needs HTML and other content. the main interface and the abstract implementation

Ipagelist.java

Import java.util.List; /** * * Paging interface, for pagination with different persistence schemes, inheriting the class and implementing the corresponding method can use * Use method: * 1.IPageList plist=new xxxpagelist ([param]);//Initialize, set parameter * 2.pLis T.dolist ()//Issue the actual query * 3.plist.getresult ()//optional, get and process the corresponding data * 4.IPager pager=new Xxxpager (pagelist);//Create Paging component class * 5.PAGER.S Ave2request (Request, "listname")//Save data, and paging component to page * 6. Place the page in the appropriate place ${pager} * Note that the SQL statement takes a paging parameter startnum, Endnum * @author WUTB1 * 2013-10-31 Morning 11:02:23 * @version 1.0.0.0 * * * public interface Ipagelist<r> {/** * issued a query * * void Doli
	St ();
	/** * The results of the query, number of pagesize number * * @return/list<r> getresult ();
	/** * Set per page size * @param pageSize */void setpagesize (int pageSize);
	/** * Get the current set of each page size * @return/int getpagesize ();
	/** * Set the current page * @param currentpage/void setcurrentpage (int currentpage);
	/** * Get Set Current page * @return/int getcurrentpage ();
	/** * Get the total number of records * @return/int gettotalcount ();
/** * Get the total number of pages * @return/int gettotalpage (); }

Ipager.java

Import Javax.servlet.http.HttpServletRequest;

/**
 * Page component's abstract
 * 1. Define page Paging component
 * 2. Return page parameter
 * Use mode
 * 1.IPager pager=new Xxxpager (ipagelist);
 * 2.pager.save2request (Request, "ListName");
 * * 
 @author wutb1
 * 2014-4-22 pm 07:36:15
 * @version 1.0.0.0/public
interface Ipager {
	
	void Setpagelist (Ipagelist plist);
	Ipagelist getpagelist ();
	
	/**
	 * Save pagelist data to page
	 * @param request
	 * @param listname
	/void Save2request ( HttpServletRequest request,string listname);
	
	/**
	 * Get page paging component, generally contains
	 * 1. Component HTML
	 * 2. Page with hidden fields
	 * 3. Component Response JS processing method * 
	 * @return
	* String Getpager ();
	
	
}

Abstractpagelist.java

Import java.util.List;
 * * Pagination abstract class, implementing common parts * 1. Processing of paging parameters pagesize, CurrentPage, TotalCount, Totalpage * 2. Query lifecycle * @non Javadocs * @see ipagelist  * * Public abstract class abstractpagelist<r> implements ipagelist<r>{private int pagesize=20;//default private
	int currentpage=1;//default 1 private int totalcount;
	private int totalpage;
	
	
	/** * Data * * Private list<r> result;
		public void Dolist () {beforequery ();
		Result=querylist ();
		Totalcount=querytotalcount ();
		Calculate the total number of pages caltotalpage ();
	Afterquery ();
	/** * Calculate total pages/private void Caltotalpage () {totalpage= (int) Math.ceil (double) totalcount/pagesize);
	/** * Abstract method, according to CurrentPage and pagesize query data * @return * * protected abstract list<r> querylist ();
	/** * Abstract method, query the total number of data * @return * * protected abstract int querytotalcount ();
	/** * Two callback functions */protected abstract void beforequery ();
	

	
	
	protected abstract void afterquery ();
	public int getpagesize () {return pageSize; } public VoID setpagesize (int pageSize) {this.pagesize = pageSize;
	public int getcurrentpage () {return currentpage;
	The public void setcurrentpage (int currentpage) {this.currentpage = CurrentPage;
	public int Gettotalcount () {return totalcount;
	public int gettotalpage () {return totalpage;
	Public list<r> GetResult () {return result; }
}

Abstractpager.java

Public abstract class Abstractpager implements ipager{
	private ipagelist pagelist;
	
	Public Ipagelist getpagelist () {return
		this.pagelist;
	}

	public void Setpagelist (Ipagelist pagelist) {
		this.pagelist=pagelist;
	}

	* *
	 * (non-javadoc)
	 * @see cn.com.jiexun.utils.ipageutil#save2request ( Javax.servlet.http.HttpServletRequest, java.lang.String)
	 *
	/public void Save2request (httpservletrequest Request, String listname) {
		Savecommon (request);
		Request.setattribute (ListName, Getpagelist (). GetResult ());
	}

	private void Savecommon (HttpServletRequest request) {
		Request.setattribute ("PageSize", Getpagelist (). GetPageSize ());
		Request.setattribute ("CurrentPage", Getpagelist (). Getcurrentpage ());
		Request.setattribute ("TotalCount", Getpagelist (). Gettotalcount ());
		Request.setattribute ("Totalpage", Getpagelist (). Gettotalpage ());
		HTML page, pagination component
		Request.setattribute ("Pager", Getpager ());
	}
Download Address

Http://pan.baidu.com/s/1hqgg8Bi



--------------------------

If you're interested in Java, swing, various frameworks, JavaScript, CSS, Linux, database programming, etc., or you're doing these things,

Welcome to join my QQ technology group: Java not lame (219345774)

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.