`
jiava9900
  • 浏览: 86789 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

重构题(1) -- 问题来临

    博客分类:
  • java
阅读更多
   
public abstract class simplepagingqueryjdbctemplate{		public static final pattern frompattern = pattern.compile("from",pattern.case_insensitive);		public static final string countsqlhead = "select count(*) from ";		public static final pattern orderbypattern = pattern.compile("order(\\s)+by",pattern.case_insensitive);		public static final pattern selectpattern = pattern.compile("select",pattern.case_insensitive);		@resource	jdbctemplate jdbctemplate;		public list <map<string,object>> query(simplequery simplequery,pager pager){		string querysql = simplequery.getquerysql();		object args = simplequery.getargs();		long count = count(simplequery);		pager.settotalobjects(count);		if(count<=pager.getfirstresult()){			return new arraylist<map<string,object>>();		}		string pagingquerysql = createpagingquerysql(querysql, pager.getfirstresult(), pager.getpagesize());		if(args == null){			return jdbctemplate.queryforlist(pagingquerysql);		}else{						return jdbctemplate.queryforlist(pagingquerysql, args);		}	}		private long count(simplequery simplequery){		string countsql = simplequery.getcountsql();		object args = simplequery.getargs();		if(countsql == null){			countsql = createcountsql(simplequery.getquerysql());		}		if(args == null){			return jdbctemplate.queryforlong(countsql);		}else{			return jdbctemplate.queryforlong(countsql, args);		}	}		private string createcountsql(string querysql){		string[] sqlparts = frompattern.split(querysql, 2);		if(sqlparts.length!=2){			throw new illegalargumentexception("the sql query maybe miss the word \"from\".");		}		string countsqlbody = cutlastorderby(sqlparts[1])[0];				return countsqlhead+countsqlbody;	}		protected string[] cutlastorderby(string sql){		matcher matcher = orderbypattern.matcher(sql);		if(!matcher.find()){			return new string[]{sql,null};		}		int	orderbyindex = matcher.start();				while(matcher.find()){			orderbyindex = matcher.start();		}		return new string[] {sql.substring(0,orderbyindex),sql.substring(orderbyindex)};	}			protected abstract string createpagingquerysql(string querysql, long firstresult, int maxresult);}


public interface pager {    long getfirstresult();    long getpagenumber();    int getpagesize();    long gettotalobjects();    long gettotalpages();    void setfirstresult(long firstresult);    void setpagenumber(long pagenumber);    void setpagesize(int pagesize);    void settotalobjects(long totalobjects);    void settotalpages(long totalpages);}


public interface simplequery {    object getargs();    string getcountsql();    string getquerysql();    void setargs(object... args);    void setcountsql(string countsql);    void setquerysql(string querysql);}


在对象村的春天软件公司,员工们使用着一个简单的分页功能...有一天,老板发话:"我们的查询功能,返回的是list <map<string,object>> 类型,客户现在觉得不满意,要求我们增加返回包装好的对象的list,正好我们有rowmapper接口,这个功能应该不难实现."于是,员工们准备向系统添加功能了. 
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics