购物项目实战学习记录(7)——分页功能实现

阅读: 评论:0

购物项目实战学习记录(7)——分页功能实现

购物项目实战学习记录(7)——分页功能实现

几个重要的类

PagingResult

分页结果的封装,结果(List)+分页信息。

Search

查询相关的类,封装查询条件+分页信息(当前页和每页记录数)。

BaseMybatisDAO

分页查询相关方法。

前端分页显示

通过PagingResult中的当前页码数总页码数每页显示数来计算最大显示页码
下面通过一些逻辑,来制定第一页最后一页中间页的显示。

<#macro paging pagingList url><#--计算最大页码--><#al % pagingList.pageSize == 0><#assign maxPageIndex = al / pagingList.pageSize><#else><#assign maxPageIndex = (al / pagingList.pageSize)?floor + 1></#if><div class="page"><#--第一页,禁用“上一页”按钮--><#al == 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 (al > 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><#--最后页,禁用“下一页”按钮--><#al == 0 || pagingList.page == maxPageIndex><span class="prev-disabled">下一页</span><#else><a href="${url}/${pagingList.page+1}">下一页</a></#if></div>
</#macro>

(1)第一页:禁用上一页。
(2)最后一页:禁用下一页。
(3)中间页(相差两端5页以上):显示...
PS:这里首页为不带页码的URL。

后台代码逻辑

Controller

	@RequestMapping(value = "/category/{urlName}/{page}", method = RequestMethod.GET)public String listDealsOfDealCategory(@PathVariable String urlName, @PathVariable Integer page, Model model,HttpServletRequest request) {DealCategory dealCategory = ByUrlName(urlName);model.addAttribute("dealCategory", dealCategory);PagingResult<Deal> pageResult = SelfAndChildrenIds(),
//				getAreaId(request), page, DealConstant.DEAL_NUM_PER_PAGE_IN_DEALS_OF_CATEGORY_PAGE);getAreaId(request), page, 6);model.addAttribute("pagingDealList", pageResult);return "/deal/category";}

说明:
(1)先拿到分类名称,根据分类名称进行查询,需要注意的是:包括他的孩子节点(细分的类别)。
(2)关键方法
获取PagingResult<Deal>
(3)拿到封装后的对象,添加到模型里面供前端使用。

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);DealsOfCategories(search);}

说明:
(1)构建Search查询类,封装查询条件(ConditionList)和分页信息。
(2)构建查询条件,发布状态、类别ID、时间、地区ID。
(3)数据库查询,返回封装的结果。

DealDAO

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

说明:调用父类的findForPage方法,第一个sqlId为计算记录总数,第二个sqlId为查询相应的记录。
疑问:分页必须查询所有的记录吗?表小一点还好,如果表很大,岂不是内存爆了。

BaseMybatisDAO

	public <T extends BaseEntity> PagingResult<T> findForPage(String countSqlId, String sqlId, Search search) {RowBounds rowBounds = new FirstRowNum(), Rows());List<T> list = template.selectList(sqlId, getConditionMap(search), rowBounds);return new PagingResult<>(count(countSqlId, search), list, Page(), Rows());}// 不用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);}

说明:
(1)RowBounds为MyBatis自带的分页处理工具,对于index之前的采取skip操作,采用limit操作限制数量。
(2)getConditionMap只是把封装在search中的ConditionList转换为Map。
(3)template.selectList查询所有语句,附带参数rowBounds,进行分页查询。【解答了上面的疑问。。。】
(4)封装一个分页结果,count(countSqlId, search)利用数据库查询总数,封装当前结果集、当前页码、显示数量。
(5)重点是RowBounds,即使不用Search也可以通过上面第二个方法进行分页查询,根据RowBounds的参数进行构造。
(6)也可以不查询总量,既返回值中构造的对象第一个参数为0,因为总量太大,前端没有显示的必要。

本文发布于:2024-02-01 07:02:19,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170674214134741.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:分页   实战   功能   项目
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23