Spring+Mybatis整合案例電商模塊三(分頁開發)

商品分頁開發

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&amp;&amp;typeCode!=''">
				and type_code like CONCAT("",#{typeCode},"%")
			</if>
			<if test="secondType!=null&amp;&amp;secondType!=''">
				and type_code = #{secondType}
			</if>
			<if test="title!=null&amp;&amp;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>

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章