一种查询参数对象的建立

在web开发时我们时常需要进行分页查询,其中就可以设计为两部分,一个是通用部分,也即各个查询对象可能都有的部分,进行抽取出来,作为一个基础查询的对象,同时针对不同的具体情况,有需要有合理的对不同情况设置查询关键字,这部分可以具体为不同的对象,这同时继承基础查询对象,这样就完成各种不同的查询对象的建立,比如如下:

一,抽象出来的共有查询特性

package com.xxx.param;


public class BaseParam {
	private Long previousPage;
	private Long nextPage;
	private Long totalRecord;
	private Long totalPage;
	private Long offsetRecord;
	
	//below is for easyui parameter
	private Long rows; 		//pageSize
	private Long page;		//current page number 
	private String sort;	//sort field name
	private String order;	//asc or desc
	
	public BaseParam() {
		this.previousPage = 1L;
		this.nextPage = 1L;
		this.totalRecord = 1L;
		this.totalPage = 1L;
		
		this.rows = 10L;
		this.page = 1L;
		this.sort = "";
		this.order = "";
	}

	public Long getPreviousPage() {
		previousPage = page - 1;
		if(previousPage < 1){
			previousPage = 1L;
		}
		return previousPage;
	}

	public void setPreviousPage(Long previousPage) {
		this.previousPage = previousPage;
	}

	public Long getNextPage() {
		if(nextPage > totalPage){
			nextPage = totalPage;
		}
		return nextPage;
	}

	public void setNextPage(Long nextPage) {
		this.nextPage = nextPage;
	}

	public Long getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(Long totalRecord) {
		if(totalRecord < 0){
			totalRecord = 0L;
		}
		this.totalRecord = totalRecord;
	}

	public Long getTotalPage() {
		if(totalRecord != null && rows != null){
			totalPage = (totalRecord % rows == 0) ? (totalRecord / rows) : (totalRecord / rows + 1);
		}else{
			totalPage = 1L;
		}
		return totalPage;
	}

	public void setTotalPage(Long totalPage) {
		this.totalPage = totalPage;
	}

	public Long getRows() {
		if(rows < 1){
			rows = 1L;
		}
		return rows;
	}

	public void setRows(Long rows) {
		this.rows = rows;
	}

	public Long getPage() {
		if(page < 1){
			page = 1L;
		}
		return page;
	}

	public void setPage(Long page) {
		this.page = page;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public Long getOffsetRecord() {
		
		/*
		//------------------------------------
		// max method/max方式
		if(page != null && rows != null){
			if(page < 1){
				page = 1L;
			}
			offsetRecord = (page - 1) * rows + 1;
		}else{
			offsetRecord = rows ; 
		}
		//------------------------------------
		*/
		
		/*
		//------------------------------------
		//top method/top方法
		if(page != null && rows != null){
			if(page < 1){
				page = 1L;
			}
			offsetRecord = page * rows ;
		}else{
			offsetRecord = rows; 
		}		
		//------------------------------------
		*/		
		
		
		//------------------------------------
		//row_number() over(order by xxx [asc|desc]) method,support ms sql2005+
		//row_number() over(order by xxx)方法
		
		if(page != null && rows != null){
			if(page < 1){
				page = 1L;
			}
			if(page > getTotalPage()){
				page = getTotalPage();
			}
			offsetRecord = (page - 1) * rows ;
		}else{
			offsetRecord = 0L; 
		}		
		//------------------------------------
		
		return offsetRecord;
	}
	
	public void setOffsetRecord(Long offsetRecord) {
		this.offsetRecord = offsetRecord;
	}

}

二,针对不同的具体情况,除了有自己的查询关键字,同时继承基本查询对象,比如国家相关信息的查询,除了有基础的查询外,还有国家相关的名称,ISO代码,域名等不同的信息查询,比如下面:

package com.xxx.param;

import com.xxx.model.CountryInfo;

public class CountryInfoParam extends BaseParam {
	private CountryInfo country;
	private Integer countryId;
	private String countryIsoCode;
	private String countryDomainName;
	private String countryName;
	private String showFlag;
	
	public CountryInfoParam() {
		super();
	}
	
	public CountryInfo getCountry() {
		return country;
	}
	public void setCountry(CountryInfo country) {
		this.country = country;
	}
	
	public Integer getCountryId() {
		return countryId;
	}
	public void setCountryId(Integer countryId) {
		this.countryId = countryId;
	}
	
	public String getCountryIsoCode() {
		return countryIsoCode;
	}
	public void setCountryIsoCode(String countryIsoCode) {
		this.countryIsoCode = countryIsoCode;
	}
	
	public String getCountryDomainName() {
		return countryDomainName;
	}
	public void setCountryDomainName(String countryDomainName) {
		this.countryDomainName = countryDomainName;
	}
	
	public String getCountryName() {
		return countryName;
	}
	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}

	public String getShowFlag() {
		return showFlag;
	}
	public void setShowFlag(String showFlag) {
		this.showFlag = showFlag;
	}
	
}

基础查询共性相同,针对不同 model 可以建立不同的 model 的查询 param 对象,基础基础查询,即可得到即灵活又有共性的 各种查询对象,在实际应用中,非常方便....

欢迎拍砖讨论... 懒人计划大笑... 


猜你喜欢

转载自blog.csdn.net/shenzhennba/article/details/51786831
今日推荐