java分页

来源:互联网 发布:windows 新增api 编辑:程序博客网 时间:2024/06/08 07:53

首先是mapper通用标签


<!-- oracle 分页头 --><sql id="pagination_Head">  <![CDATA[      select * from ( select row_.*, rownum rownum_ from (      ]]></sql><!-- oracle 分页尾 --><sql id="pagination_Tail">  <![CDATA[      ) row_ where rownum <= #{pager.pageSize}* #{pager.offset} ) where rownum_ >= #{pager.firstResult} + 1      ]]></sql><!-- count * from --><sql id="count_Start_Head">  <![CDATA[      select count(*) from (      ]]></sql><sql id="count_Start_Tail">  <![CDATA[      ) countObj      ]]></sql>
<select id="searchAllPayment" resultMap="BaseResultMap">  <include refid="pagination_Head"/>  <include refid="search_all_payment"/>  <include refid="pagination_Tail"/>  order by aoid desc,purchase_order_id desc,pay_Id desc,vip_Pay_Id desc,vfp_Pay_Id desc</select><select id="searchAllPaymentCount" resultType="long">  <include refid="count_Start_Head"/>  <include refid="search_all_payment"/>  <include refid="count_Start_Tail"/></select>


然后  是Service

@Overridepublic Pager<FullProcess> searchReceiveFullProcess(Pager<FullProcess> pager, FullProcess fullProcess) {    List<FullProcess> fullProcesses = fullProcessDAO.searchReceiveFullProcess(pager, fullProcess);    Long fullProcessesCount = fullProcessDAO.searchReceiveFullProcessCount(fullProcess);    return Pager.cloneFromPager(pager, fullProcessesCount, fullProcesses);}


再次 controller


@RequestMapping("/searchReceiveFullProcess")public String searchReceiveFullProcess(Model model, HttpServletRequest request, Long offset, Long pageSize, FullProcess fullProcess) {    fullProcess = getQueryCriteria(fullProcess);    Pager<FullProcess> pager = new Pager<FullProcess>();    pager.setOffset(null == offset ? pager.getOffset() : offset);    pager.setPageSize(null == pageSize ? pager.getPageSize() : pageSize);    Pager<FullProcess> fullProcessPager = fullProcessService.searchReceiveFullProcess(pager, fullProcess);    fullProcessPager.setUri(request.getRequestURI());    model.addAttribute("fullProcessPager", fullProcessPager);    return "report/receive_full_process_list";}


最后是pager类

public class Pager<T> implements Serializable {    public static final Long DEFAULT_PAGE_SIZE = Long.valueOf(20L);    private static final long serialVersionUID = 609222305391683918L;    private List<T> records = new ArrayList();    // Item总数量    @Getter @Setter    private Long total = Long.valueOf(0L);    // 当前第几页    @Getter    private Long offset = Long.valueOf(1L);    private Long pageSize;    @Setter @Getter    private String uri;    // 使用dialog弹窗时,父窗口的id    @Getter @Setter    private String parentWindowId;    private String orderProperty;    private String order;    private boolean countTotal;    public Pager() {        this.pageSize = DEFAULT_PAGE_SIZE;        this.orderProperty = "";        this.order = "";        this.countTotal = true;    }    public List<T> getRecords() {        return this.records;    }    public void setRecords(List<T> records) {        this.records = records;    }    public void setOffset(Long offset) {        if (offset.longValue() <= 0L) {            this.offset = Long.valueOf(1L);        } else {            this.offset = offset;        }    }    public Long getPageSize() {        return this.pageSize;    }    public void setPageSize(Long pageSize) {        if (pageSize.longValue() <= 0L) {            this.pageSize = Long.valueOf(1L);        } else {            this.pageSize = pageSize;        }    }    public boolean isCountTotal() {        return this.countTotal;    }    public void setCountTotal(boolean countTotal) {        this.countTotal = countTotal;    }    public Long getTotalPages() {        if (this.getTotal().longValue() == 0L) {            return Long.valueOf(1L);        } else {            Long div = Long.valueOf(this.getTotal().longValue() / this.getPageSize().longValue());            Long sub = Long.valueOf(this.getTotal().longValue() % this.getPageSize().longValue());            return sub.longValue() == 0L ? div : Long.valueOf(div.longValue() + 1L);        }    }    public boolean isOrderBySetted() {        return StringUtils.isNotBlank(this.order) && StringUtils.isNotBlank(this.orderProperty);    }    public Long getFirstResult() {        return Long.valueOf((this.getOffset().longValue() - 1L) * this.getPageSize().longValue());    }    public Long getFirstResultExt() {        Long firstPage = this.getFirstResult();        return Long.valueOf(firstPage.longValue() <= 0L ? 0L : firstPage.longValue() - 1L);    }    public String getOrderProperty() {        return this.orderProperty;    }    public void setOrderProperty(String orderProperty) {        this.orderProperty = orderProperty;    }    public String getOrder() {        return this.order;    }    public void setOrder(String order) {        String lowcaseOrderDir = StringUtils.lowerCase(order);        String[] orderDirs = StringUtils.split(lowcaseOrderDir, ',');        String[] arr = orderDirs;        int len = orderDirs.length;        for (int i = 0; i < len; ++i) {            String orderDirStr = arr[i];            if (!StringUtils.equals("desc", orderDirStr) && !StringUtils.equals("asc", orderDirStr)) {                throw new IllegalArgumentException("排序方向" + orderDirStr + "不是合法值");            }        }        this.order = lowcaseOrderDir;    }    public List<Sort> getSort() {        String[] orderBys = StringUtils.split(this.orderProperty, ',');        String[] orderDirs = StringUtils.split(this.order, ',');        Validate.isTrue(orderBys.length == orderDirs.length, "分页多重排序参数中,排序字段与排序方向的个数不相等", new Object[0]);        ArrayList orders = new ArrayList();        for (int i = 0; i < orderBys.length; ++i) {            orders.add(new Pager.Sort(orderBys[i], orderDirs[i]));        }        return orders;    }    public static <X, M> Pager<M> cloneFromPager(Pager<X> pager) {        Pager result = new Pager();        result.setCountTotal(pager.isCountTotal());        result.setOffset(pager.getOffset());        result.setOrder(pager.getOrder());        result.setOrderProperty(pager.getOrderProperty());        result.setPageSize(pager.getPageSize());        result.setTotal(pager.getTotal());        return result;    }    public static <X> Pager<X> cloneFromPager(Pager<X> pager, long total, List<X> records) {        Pager result = cloneFromPager(pager);        result.setTotal(Long.valueOf(total));        result.setRecords(records);        return result;    }    public static class Sort {        public static final String ASC = "asc";        public static final String DESC = "desc";        private final String property;        private final String dir;        public Sort(String property, String dir) {            this.property = property;            this.dir = dir;        }        public String getProperty() {            return this.property;        }        public String getDir() {            return this.dir;        }    }}




0 0