Shopping project combat learning record (7) - paging function to achieve

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u012525096/article/details/87966921

Several important classes

PagingResult

Package paged results, the results (List) + paging information.

Search

Relevant category, query package + paging information (number of records per page and this page).

BaseMybatisDAO

Paging relevant methods.

The front page display

PagingResult by the 当前页码数, 总页码数, 每页显示数is calculated 最大显示页码.
By following some logic to develop 第一页, 最后一页, 中间页it is displayed.

<#macro paging pagingList url>
	<#--计算最大页码-->
    <#if pagingList.total % pagingList.pageSize == 0>
        <#assign maxPageIndex = pagingList.total / pagingList.pageSize>
    <#else>
        <#assign maxPageIndex = (pagingList.total / pagingList.pageSize)?floor + 1>
    </#if>

	<div class="page">
        <#--第一页,禁用“上一页”按钮-->
        <#if pagingList.total == 0 || pagingList.page == 1>
        	<span class="prev-disabled">上一页</span>
        <#else>
        	<#if pagingList.page == 2>
            	<a href="${url}">上一页</a>
            <#else>
            	<a href="${url}/${pagingList.page-1}">上一页</a>
            </#if>
        </#if>

        <#--第一页-->
        <#if (pagingList.total > 0)>
            <a href="${url}" <#if pagingList.page == 1>class="current_page"</#if>>1</a>
        </#if>

        <#--如果不只有一页-->
        <#if (maxPageIndex > 1)>
            <#--如果当前页往前查3页不是第2页-->
            <#if ((pagingList.page - 3) > 2)>
                <span class="text"></span>
            </#if>

            <#--当前页的前3页和后3页-->
            <#list (pagingList.page - 3)..(pagingList.page + 3) as index>
                <#--如果位于第一页和最后一页之间-->
                <#if (index > 1) && (index < maxPageIndex)>
                    <a href="${url}/${index}" <#if pagingList.page == index>class="current_page"</#if>>${index}</a>
                </#if>
            </#list>

            <#--如果当前页往后查3页不是倒数第2页-->
            <#if (pagingList.page + 3) < (maxPageIndex - 1)>
                <span class="text"></span>
            </#if>

            <#--最后页-->
            <a href="${url}/${maxPageIndex}" <#if pagingList.page == maxPageIndex>class="current_page"</#if>>${maxPageIndex}</a>
        </#if>

        <#--最后页,禁用“下一页”按钮-->
        <#if pagingList.total == 0 || pagingList.page == maxPageIndex>
            <span class="prev-disabled">下一页</span>
        <#else>
            <a href="${url}/${pagingList.page+1}">下一页</a>
        </#if>
    </div>
</#macro>

(1) First page: Previous disabled.
(2) the last page: Disable the next page.
(3) middle page (a difference of more than 5 ends): displays ....
PS: Here is the URL home without a page number.

Code-behind logic

Controller

	@RequestMapping(value = "/category/{urlName}/{page}", method = RequestMethod.GET)
	public String listDealsOfDealCategory(@PathVariable String urlName, @PathVariable Integer page, Model model,
										  HttpServletRequest request) {
		DealCategory dealCategory = dealCategoryService.getByUrlName(urlName);

		model.addAttribute("dealCategory", dealCategory);
		PagingResult<Deal> pageResult = dealService.getDealsOfCategories(dealCategory.getSelfAndChildrenIds(),
//				getAreaId(request), page, DealConstant.DEAL_NUM_PER_PAGE_IN_DEALS_OF_CATEGORY_PAGE);
				getAreaId(request), page, 6);
		model.addAttribute("pagingDealList", pageResult);
		return "/deal/category";
	}

Description:
(1) get the first category name, query name based on the classification, should be noted that: including his child node (subdivision category).
(2) key method dealService.getDealsOfCategories(dealCategory.getSelfAndChildrenIds(),getAreaId(request), page, 6);
to obtain PagingResult<Deal>.
(3) get packaged objects, which is added to the model used for the front end.

Service

    public PagingResult<Deal> getDealsOfCategories(List<Long> categoryIds, Long areaId, int page, int rows) {
        Search search = new Search();
        List<Condition> conditionList = new ArrayList<>();
        conditionList.add(new Condition("publishStatus", DealConstant.DEAL_PUBLISH_STATUS_PUBLISH)); // 发布状态为已发布

        conditionList.add(new Condition("categoryIdList", categoryIds));
        conditionList.add(new Condition("nowTime", new Date()));
        conditionList.add(new Condition("areaId", areaId));
        search.setConditionList(conditionList);
        search.setPage(page);
        search.setRows(rows);

        return dealDAO.getDealsOfCategories(search);
    }

Note:
(1) Construction of Searcha query, a package query (ConditionList) and paging information.
(2) build the query, publishing status, category ID, time, area ID.
(3) a database query that returns the result of the package.

DealDAO

    public PagingResult<Deal> getDealsOfCategories(Search search) {
        return super.findForPage(MAPPER_NAMESPACE + ".countDealsOfCategories", MAPPER_NAMESPACE
                + ".selectDealsOfCategories", search);
    }

Description: call the parent class findForPagemethod, to calculate the total number of first sqlId record, the second record corresponding sqlId query.
Question: page must query all records? Fortunately, smaller table, if the table is large, would not burst memory.

BaseMybatisDAO

	public <T extends BaseEntity> PagingResult<T> findForPage(String countSqlId, String sqlId, Search search) {
		RowBounds rowBounds = new RowBounds(search.getFirstRowNum(), search.getRows());
		List<T> list = template.selectList(sqlId, getConditionMap(search), rowBounds);
		return new PagingResult<>(count(countSqlId, search), list, search.getPage(), search.getRows());
	}
	// 不用Search条件如何分页
	public <T extends BaseEntity> PagingResult<T> findForPage(String countSqlId, String sqlId, int page, int rows, Map<String, Object> params) {
		RowBounds rowBounds = new RowBounds((page - 1) * rows, rows);
		List<T> data = template.selectList(sqlId, params, rowBounds);
		return new PagingResult<>(count(countSqlId, params), data, page, rows);
	}

Note:
(1) RowBoundsas MyBatis own paging processing tools, for the index before taking skip operation, using quantitative limit limiting operation.
(2) getConditionMapjust ConditionList encapsulated in the search into a Map.
(3) template.selectListto query all statements, with parameters rowBounds, paging query. [Answer the above questions. . . ]
(4) a package paged results, count(countSqlId, search)use of the total number of database query, the result set of the current package, the current page number of the display.
(5) the focus is RowBounds, even without Search queries can be paged by the second method above, according to the RowBoundsconstructed arguments.
(6) may not query the total amount of both the return object value for the first parameter configuration 0, since the total amount is too large, the distal end need not shown.

Guess you like

Origin blog.csdn.net/u012525096/article/details/87966921