package page; import java.util.Map; public class Column { private Object data; private Object name; private Object searchable; private Object orderable; private Map<String,Object> search; public Object getData() { return data; } public void setData(Object data) { this.data = data; } public Object getName() { return name; } public void setName(Object name) { this.name = name; } public Object getSearchable() { return searchable; } public void setSearchable(Object searchable) { this.searchable = searchable; } public Object getOrderable() { return orderable; } public void setOrderable(Object orderable) { this.orderable = orderable; } public Map<String, Object> getSearch() { return search; } public void setSearch(Map<String, Object> search) { this.search = search; } }
PageCondition.java
package page; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class PageCondition { private final String[] rules={"draw","start","length","search","order","columns"}; private JSONObject jsonObj; public PageCondition(JSONObject jsonObj){ this.jsonObj=jsonObj; } public int draw(){ if(containKey("draw")){ return getInt("draw"); } throw new IllegalArgumentException("没有order"); } public int start(){ if(containKey("start")){ return getInt("start"); } throw new IllegalArgumentException("没有start"); } public int length(){ if(containKey("length")){ return getInt("length"); } throw new IllegalArgumentException("没有length"); } public Map<String,String> search(){ if(containKey("search")){ return getMap("search"); } throw new IllegalArgumentException("没有search"); } public List<Map<String,String>> order(){ List<Map<String,String>> list=new ArrayList<Map<String,String>>(); if(containKey("order")){ JSONArray jsonArray=jsonObj.getJSONArray("order"); for(int i=0;i<jsonArray.size();i++){ JSONObject dataObject=jsonArray.getJSONObject(i); list.add(getMap(dataObject)); } return list; } throw new IllegalArgumentException("没有order"); } public List<Column> columns(){ List<Column> list=new ArrayList<Column>(); if(containKey("columns")){ JSONArray jsonArray=jsonObj.getJSONArray("columns"); for(int i=0;i<jsonArray.size();i++){ JSONObject dataObject=jsonArray.getJSONObject(i); Column columnData=(Column)JSONObject.toBean(dataObject,Column.class); list.add(columnData); } return list; } throw new IllegalArgumentException("没有columns"); } public Map<String,String> outSearch(){ Set<String> keys=jsonObj.keySet(); Set<String> keyCopy=new HashSet<String>(); keyCopy.addAll(keys); keyCopy.removeAll(Arrays.asList(rules)); Iterator<String> i=keyCopy.iterator(); Map<String,String> hashMap=new HashMap<String,String>(); while(i.hasNext()){ String key=i.next(); String value=jsonObj.getString(key); hashMap.put(key, value); } return hashMap; } public boolean serverSide(){ if(jsonObj.isEmpty()) return false; return true; } ////////////////////// private int getInt(String target){ return jsonObj.getInt(target); } private Map<String,String> getMap(String target){ JSONObject jsonMap=jsonObj.getJSONObject(target); return getMap(jsonMap); } private Map<String,String> getMap(JSONObject jsonMap){ Map<String,String> map=new HashMap<String,String>(); Iterator<String> iterator=jsonMap.keys(); while(iterator.hasNext()){ String key=iterator.next(); String value=jsonMap.getString(key); map.put(key, value); } return map; } private boolean containKey(Object key){ if(jsonObj.containsKey(key)){ return true; } return false; } }
PageFilter.java
package page; import java.util.Map; public interface PageFilter { public String execute(String hsql,Map<String,String> parameters,PageCondition condition); }
PageResult.java
package page; import java.util.List; public class PageResult<T> { public int draw; public int recordsTotal; public int recordsFiltered; public List<T> data; public int getDraw() { return draw; } public void setDraw(int draw) { this.draw = draw; } public int getRecordsTotal() { return recordsTotal; } public void setRecordsTotal(int recordsTotal) { this.recordsTotal = recordsTotal; } public int getRecordsFiltered() { return recordsFiltered; } public void setRecordsFiltered(int recordsFiltered) { this.recordsFiltered = recordsFiltered; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } }
XXXController.java controller层 使用 springmvc
package page; import net.sf.json.JSONObject; public class XXXController { @RequestMapping(value="/XXXXXXX") @ResponseBody public PageResult<RtnClass> doRequest(@RequestBody JSONObject jsonObj){ PageCondition condition=new PageCondition(jsonObj); PageResult<RtnClass> result=XXXservice.doService(condition); } }
XXXService.java service层代码
package page; import java.util.Map; import net.sf.json.JSONObject; public class XXXService { public void doService(PageCondition condition){ String hsql="select t from RtnClass t where 1=1 "; String hsqlCount="select count(*) from RtnClass t where 1=1"; PageFilter filter=new PageFilter(){ @Override public String execute(String hsql, Map<String, String> parameters, PageCondition condition) { if(StringUtils.hasText(parameters.get("param"))){ hsql+=" and t.param = "+parameters.get("param"); } return hsql; } }; } }
XXXDao.java dao层代码 用的是hibernate
package page; import java.util.List; import java.util.Map; import javax.management.Query; import org.apache.commons.lang.StringUtils; public class XXXDao { public int queryRecordsTotal(String hsql,PageCondition condition){ Query query=getSession().createQuery(hsql); return ((Number)query.uniqueResult()).intValue(); } public int queryRecordsFiltered(String hsql,PageCondition condition,PageFilter filter){ Map<String,String> parameters=condition.outSearch(); hsql=filter.execute(hsql, parameters, condition); Query query=getSession().createQuery(hsql); return ((Number)query.uniqueResult()).intValue(); } public <T> List<T> queryPage(String hsql,PageCondition condition){ Query query=getSession().createQuery(hsql); int start=condition.start(); int length=condition.length(); query.setFisrtResult(start); query.setMaxResults(length); return query.list(); } public <T> PageResult<T> queryForPage(String hsql,String hsqlCount,PageCondition condition,PageFilter filter){ PageResult<T> result=new PageResult<T>(); result.setDraw(condition.draw()); int recordsTotal=queryRecordsTotal(hsqlCount,condition); int recordsFiltered=queryRecordsFiltered(hsqlCount,condition,filter); result.setRecordsTotal(recordsTotal); result.setRecordsFiltered(recordsFiltered); //filter Map<String,String> parameters=condition.outSearch(); hsql=filter.execute(hsqlCount, parameters, condition); //order StringBuilder orderSb=new StringBuilder(" order by"); List<Map<String,String>> order=condition.order(); for(Map<String,String> orderMap:order){ String column=orderMap.get("column"); String dir=orderMap.get("dir"); int columnNum=Integer.parseInt(column); String name=condition.columns().get(columnNum).getName().toString(); if(StringUtils.isNotEmpty(name)){ orderSb.append(" "+name+" "+dir+" , " ); } } String orderStr=orderSb.toString(); if(orderStr.equals(" order by")){ orderStr=""; }else{ orderStr=orderStr.substring(0,orderStr.lastIndexOf(",")); } hsql+=orderStr; //page List<T> data=queryPage(hsql,condition); result.setData(data); return result; } }
jsonobjct jar包可以在附件中下载,代码仅供参考