最近做的项目都用到Pageable分页,感觉挺好用的,就把它记下来,以后去别的地方也会用的着
想用Pageable,要导入jar:spring-data-commons-1.7.0.RELEASE.jar
先建个Page的实体类用于后面的操作
public class Pager implements Serializable { private static final long serialVersionUID = 1236926132859752284L; // 倒序还是顺序 private String order; // 排序的字段 private String sort; // 当前页 private int page = 1; // 每页分页的行数 private int rows = 10; private Object[] values; // 数据的条数 private long total; // 总页数 private long allPage; public Pager() { } public Pager(int page, int rows, String sort, String order, Object[] values) { this.page = page; this.rows = rows; this.sort = sort; this.order = order; this.values = values; } public Object[] getValues() { return values; } public void setValues(Object[] values) { this.values = values; } public String getOrder() { return order; } public void setOrder(String order) { this.order = order; } public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public long getAllPage() { return allPage; } public void setAllPage() { if (total % this.rows == 0) { allPage = total / rows; } else { allPage = total / rows + 1; } } /** * * 方法描述:获取分页对象 创建人:dujiewei 创建时间:2015-4-15 下午5:07:22 */ public static Pageable getPageable(int page, int rows) { Pageable pageable = new PageRequest(page, rows); return pageable; } /** * * 方法描述:获取分页对象 */ public static Pageable getPageable(Pager pager) { int page = pager.getPage(); int rows = pager.getRows(); String sort = pager.getSort(); String order = pager.getOrder(); Pageable pageable = null; if (null == sort || "".equals(sort)) { pageable = new PageRequest(page, rows); } else { pageable = new PageRequest(page, rows, Sort.Direction.fromStringOrNull(order), sort); } return pageable; } @Override public String toString() { return "Pager [order=" + order + ", sort=" + sort + ", page=" + page + ", rows=" + rows + ", values=" + Arrays.toString(values) + ", total=" + total + ", allPage=" + allPage + "]"; } }
上面的代码有一段代码写的是获取分页对象
*/ public static Pageable getPageable(int page, int rows) { Pageable pageable = new PageRequest(page, rows); return pageable; } /** * * 方法描述:获取分页对象 */ public static Pageable getPageable(Pager pager) { int page = pager.getPage(); int rows = pager.getRows(); String sort = pager.getSort(); String order = pager.getOrder(); Pageable pageable = null; if (null == sort || "".equals(sort)) { pageable = new PageRequest(page, rows); } else { pageable = new PageRequest(page, rows, Sort.Direction.fromStringOrNull(order), sort); } return pageable; }
这段代码才是关键 它是将page对象封装进pageable里面。有了它,我们可以在action或者controller里面写业务逻辑的时候直接使用。demo如下:
比如说我们在用springmvc的时候用到分页
@RequestMapping("/getFinanceLog") public ModelAndView getSettleList(Pager page,FinanceLogVO financeLog, HttpServletRequest request,HttpServletResponse response) { ModelAndView view=new ModelAndView(); Map<String, Object> map=new HashMap<String, Object>(); map.put("ORDER_ID", financeLog.getORDER_ID()); //根据订单编号去查询 map.put("PAY_TYPE", financeLog.getPAY_TYPE()); //根据支付方式去查询 map.put("beginDate", request.getParameter("beginDate")); map.put("endDate", request.getParameter("endDate")); //时间条件查询 map.put("BUSI_TYPE", financeLog.getBUSI_TYPE());//根据业务类型查询 map.put("TRADE_ID",financeLog.getTRADE_ID()); //根据财务流水号查询 if(StrUtil.isNotNull(request.getParameter("beginDate"))&&StrUtil.isNotNull(request.getParameter("endDate"))){ map.put("allDate","allDate");//如果前台两个参数都传过来,那么在数据库里面就用 between and来取时间区间 } if(StrUtil.isNotNull(request.getParameter("beginDate"))&&StrUtil.isNull(request.getParameter("endDate"))){ map.put("startDate","startDate"); } if(StrUtil.isNotNull(request.getParameter("endDate"))&&StrUtil.isNull(request.getParameter("beginDate"))){ map.put("endsDate","endsDate"); } page.setRows(PAGE_SIZE); //每页多少数据 Pageable pageable = Pager.getPageable(page); //页数封装 String userId = (String) request.getSession().getAttribute("UID"); try { if (StrUtil.isNotNull(userId)) { UserVO user = LoginHelper.getSessionForUser(request); //获取登录角色 String userType = user.getUSER_TYPE(); if(userType.equals(Constant.USER_TYPE.SHOP)){ //如果查询出来的用户角色是商家 StoreVO store = storeService.selectStoreByUserId(user.getUSER_ID()); //根据登录角色的userId查询商铺 map.put("STORE_ID", store.getSTORE_ID()); //如果是商家的话,只查询商家的订单流水 } Map<String, Object> list=financeLogService.selectLogList(map,pageable); //查询 view.addObject("logList", list.get("rows")); view.addObject("page_info", list.get("page_info")); view.addObject("param", map); view.setViewName("zhaiqqadmin/finance/financeLog_list"); }else{ // 跳转到登录页面 view.setViewName("/zhaiqqadmin/login"); } } catch (Exception e) { logger.error(e); } return view; }
将Pager.getPageable(page)封装进pageable里面,pageable封装的是page对象。里面的各种参数可以自己设定。在查询的时候要将pageable对象传进去。这样就可以分页了。