商品分页开发
utils/pager.java
package com.imooc.shop.utils;
//分页实体
public class Pager {
//1.当前页码
private int pageIndex=1;
//2.一页需要展示多少条数据
private int pageSize=3;
//3.当前条件下总的数据量
private int totalCount;
//4.总共可以分多少页
private int totalPages;
public int getPageIndex() {
//取页码的时候,做一些判断
pageIndex = pageIndex<=0?1:pageIndex;
//判断页码是否越界了
pageIndex = pageIndex>=getTotalPages()?getTotalPages():pageIndex;
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPages() {
//总页数
//根据总数据量和每页最多展示多少来确定的
return (this.getTotalCount()-1)/this.getPageSize()+1;
}
//分页的第一个参数 从哪个位置开始
public int getFirstParam(){
return (this.getPageIndex()-1)*this.getPageSize();
}
}
list.jsp
<!-- 分页标签 -->
<nav>
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/list?method=getAll&typeCode=${typeCode}&secondType=${secondType}&title=${title}&pageIndex=1">首页</a></li>
<li><a href="${pageContext.request.contextPath}/list?method=getAll&typeCode=${typeCode}&secondType=${secondType}&title=${title}&pageIndex=${pager.pageIndex -1}">上一页</a></li>
<li><a href="${pageContext.request.contextPath}/list?method=getAll&typeCode=${typeCode}&secondType=${secondType}&title=${title}&pageIndex=${pager.pageIndex + 1}">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/list?method=getAll&typeCode=${typeCode}&secondType=${secondType}&title=${title}&pageIndex=${pager.totalPages}">尾页</a></li>
<li>
<a>总数据量${pager.totalCount},当前<span style="color: red;">${pager.pageIndex}</span>/${pager.totalPages}</a>
</li>
</ul>
</nav>
ListServlet
private void getAll() throws ServletException, IOException {
//考虑分页查询
Pager pager = new Pager();
//看是否传入了分页参数的页码
String pageIndex = request.getParameter("pageIndex");
if(!StringUtils.isEmpty(pageIndex)){
int pSize = Integer.valueOf(pageIndex);
pager.setPageIndex(pSize);
}
//接受一级类型编号查询
String typeCode =request.getParameter("typeCode");
//接受二级类型编号查询
String secondType = request.getParameter("secondType");
//接受标题搜索
String title = request.getParameter("title");
//转回界面
request.setAttribute("title",title);
//转回到界面,使二级类型选中
request.setAttribute("secondType",secondType);
//根据一级类型查询对应的二级类型
if(!StringUtils.isEmpty(typeCode)){
List<ArticleType> secondTypes = shopService.loadSecondTypes(typeCode);
request.setAttribute("typeCode",typeCode);
request.setAttribute("secondTypes",secondTypes);
}
//1.查询所有的一级类型数据
List<ArticleType> firstArticleTypes = shopService.loadFirstArticleTypes();
//2.查询所有的商品信息
List<Article> articles = shopService.searchArticles(typeCode,secondType,title,pager);
request.setAttribute("articleTypes",shopService.getArticleTypes());
request.setAttribute("firstArticleTypes",firstArticleTypes);
request.setAttribute("pager",pager);
request.setAttribute("articles",articles);
request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request,response);
}
shopService
shopServiceImpl
ArticleMapper
ArticleMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.shop.repository.ArticleMapper">
<sql id="pageWhere">
<where>
<if test="typeCode!=null&&typeCode!=''">
and type_code like CONCAT("",#{typeCode},"%")
</if>
<if test="secondType!=null&&secondType!=''">
and type_code = #{secondType}
</if>
<if test="title!=null&&title!=''">
and title like CONCAT("%",#{title},"%")
</if>
</where>
</sql>
<select id="searchArticles" resultType="Article">
select * from ec_article
<include refid="pageWhere"/>
LIMIT #{pager.firstParam},#{pager.pageSize}
</select>
<select id="count" resultType="int">
select count(*) from ec_article
<include refid="pageWhere"/>
</select>
</mapper>