欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SSM框架實(shí)現(xiàn)分頁(yè)和搜索分頁(yè)的示例代碼

 更新時(shí)間:2018年03月26日 13:48:38   作者:Java團(tuán)長(zhǎng)  
本篇文章主要介紹了SSM框架實(shí)現(xiàn)分頁(yè)和搜索分頁(yè)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

分頁(yè)是Java Web項(xiàng)目常用的功能,昨天在Spring MVC中實(shí)現(xiàn)了簡(jiǎn)單的分頁(yè)操作和搜索分頁(yè),在此記錄一下。使用的框架為(MyBatis+SpringMVC+Spring)。

首先我們需要一個(gè)分頁(yè)的工具類:

1.分頁(yè)

import java.io.Serializable; 
/** 
* 分頁(yè) 
*/ 
public class Page implements Serializable {  
  private static final long serialVersionUID = -3198048449643774660L;  
  private int pageNow = 1; // 當(dāng)前頁(yè)數(shù)  
  private int pageSize = 4; // 每頁(yè)顯示記錄的條數(shù)  
  private int totalCount; // 總的記錄條數(shù) 
  private int totalPageCount; // 總的頁(yè)數(shù)  
  @SuppressWarnings("unused") 
  private int startPos; // 開始位置,從0開始 
 
  @SuppressWarnings("unused") 
  private boolean hasFirst;// 是否有首頁(yè) 
 
  @SuppressWarnings("unused") 
  private boolean hasPre;// 是否有前一頁(yè) 
 
  @SuppressWarnings("unused") 
  private boolean hasNext;// 是否有下一頁(yè) 
 
  @SuppressWarnings("unused") 
  private boolean hasLast;// 是否有最后一頁(yè)    
  /** 
  * 通過構(gòu)造函數(shù) 傳入 總記錄數(shù) 和 當(dāng)前頁(yè) 
  * @param totalCount 
  * @param pageNow 
  */ 
  public Page(int totalCount, int pageNow) { 
    this.totalCount = totalCount; 
    this.pageNow = pageNow; 
  } 
   
  /** 
  * 取得總頁(yè)數(shù),總頁(yè)數(shù)=總記錄數(shù)/總頁(yè)數(shù) 
  * @return 
  */ 
  public int getTotalPageCount() { 
    totalPageCount = getTotalCount() / getPageSize(); 
    return (totalCount % pageSize == 0) ? totalPageCount 
        : totalPageCount + 1; 
  } 
 
  public void setTotalPageCount(int totalPageCount) { 
    this.totalPageCount = totalPageCount; 
  } 
 
  public int getPageNow() { 
    return pageNow; 
  } 
 
  public void setPageNow(int pageNow) { 
    this.pageNow = pageNow; 
  } 
 
  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; 
  } 
  /** 
  * 取得選擇記錄的初始位置 
  * @return 
  */ 
  public int getStartPos() { 
    return (pageNow - 1) * pageSize; 
  } 
 
  public void setStartPos(int startPos) { 
    this.startPos = startPos; 
  } 
 
  /** 
  * 是否是第一頁(yè) 
  * @return 
  */ 
  public boolean isHasFirst() { 
    return (pageNow == 1) ? false : true; 
  } 
 
  public void setHasFirst(boolean hasFirst) { 
    this.hasFirst = hasFirst; 
  } 
  /** 
  * 是否有首頁(yè) 
  * @return 
  */ 
  public boolean isHasPre() { 
    // 如果有首頁(yè)就有前一頁(yè),因?yàn)橛惺醉?yè)就不是第一頁(yè) 
    return isHasFirst() ? true : false; 
  } 
 
  public void setHasPre(boolean hasPre) { 
    this.hasPre = hasPre; 
  } 
  /** 
  * 是否有下一頁(yè) 
  * @return 
  */ 
  public boolean isHasNext() { 
    // 如果有尾頁(yè)就有下一頁(yè),因?yàn)橛形岔?yè)表明不是最后一頁(yè) 
    return isHasLast() ? true : false; 
  } 
 
  public void setHasNext(boolean hasNext) { 
    this.hasNext = hasNext; 
  } 
  /** 
  * 是否有尾頁(yè) 
  * @return 
  */ 
  public boolean isHasLast() { 
    // 如果不是最后一頁(yè)就有尾頁(yè) 
    return (pageNow == getTotalCount()) ? false : true; 
  } 
 
  public void setHasLast(boolean hasLast) { 
    this.hasLast = hasLast; 
  }  
}

有了這個(gè)工具類后,首先編寫MyBatis的XxxxMapper.xml配置文件中的SQL語(yǔ)句,如下:

<!-- 分頁(yè)SQL語(yǔ)句 --> 
<select id="selectProductsByPage" resultMap="返回值類型"> 
 select  
 * 
 from 表名 WHERE user_id = #{userId,jdbcType=INTEGER} limit #{startPos},#{pageSize}  
</select> 
<!-- 取得記錄的總數(shù) --> 
<select id="getProductsCount" resultType="long"> 
 SELECT COUNT(*) FROM 表名 WHERE user_id = #{userId,jdbcType=INTEGER}  
</select>

此處我們可以看到,2個(gè)<select>需要分別傳入3個(gè)和1個(gè)參數(shù),此時(shí)在對(duì)應(yīng)的DAO文件IXxxxDao中編寫接口來編寫對(duì)應(yīng)的方法,方法名和mapper.xml中的id屬性值一致:

/** 
* 使用注解方式傳入多個(gè)參數(shù),用戶產(chǎn)品分頁(yè),通過登錄用戶ID查詢 
* @param page 
* @param userId 
* @return startPos},#{pageSize} 
*/ 
public List<Products> selectProductsByPage(@Param(value="startPos") Integer startPos,@Param(value="pageSize") Integer pageSize,@Param(value="userId") Integer userId); 
 
/** 
* 取得產(chǎn)品數(shù)量信息,通過登錄用戶ID查詢 
* @param userId 
* @return 
*/ 
public long getProductsCount(@Param(value="userId") Integer userId);

接口定義完成之后需要編寫相應(yīng)的業(yè)務(wù)接口和實(shí)現(xiàn)方法,在接口中定義這樣一個(gè)方法,然后實(shí)現(xiàn)類中覆寫一下:

/** 
  * 分頁(yè)顯示商品 
  * @param request 
  * @param model 
  * @param loginUserId 
  */ 
  void showProductsByPage(HttpServletRequest request,Model model,int loginUserId);

接下來實(shí)現(xiàn)類中的方法就是要調(diào)用DAO層和接受Controller傳入的參數(shù),進(jìn)行業(yè)務(wù)邏輯的處理,request用來獲取前端傳入的參數(shù),model用來向JSP頁(yè)面返回處理結(jié)果。

@Override 
public void showProductsByPage(HttpServletRequest request, Model model,int loginUserId) { 
  String pageNow = request.getParameter("pageNow"); 
 
  Page page = null; 
 
  List<ProductWithBLOBs> products = new ArrayList<ProductWithBLOBs>(); 
 
  int totalCount = (int) productDao.getProductsCount(loginUserId); 
 
  if (pageNow != null) { 
    page = new Page(totalCount, Integer.parseInt(pageNow)); 
    allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId); 
  } else { 
    page = new Page(totalCount, 1); 
    allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId); 
  } 
 
  model.addAttribute("products", products); 
  model.addAttribute("page", page); 
}

接下來是控制器的編寫,當(dāng)用戶需要跳轉(zhuǎn)到這個(gè)現(xiàn)實(shí)產(chǎn)品的頁(yè)面時(shí),就需要經(jīng)過這個(gè)控制器中相應(yīng)方法的處理,這個(gè)處理過程就是調(diào)用業(yè)務(wù)層的方法來完成,然后返回結(jié)果到JSP動(dòng)態(tài)顯示,服務(wù)器端生成好頁(yè)面后傳給客戶端(瀏覽器)現(xiàn)實(shí),這就是一個(gè)MVC過程。

/** 
* 初始化 “我的產(chǎn)品”列表 JSP頁(yè)面,具有分頁(yè)功能 
* 
* @param request 
* @param model 
* @return 
*/ 
@RequestMapping(value = "映射路徑", method = RequestMethod.GET) 
public String showMyProduct(HttpServletRequest request, Model model) { 
  // 取得SESSION中的loginUser 
  User loginUser = (User) request.getSession().getAttribute("loginUser"); 
  // 判斷SESSION是否失效 
  if (loginUser == null || "".equals(loginUser)) { 
    return "redirect:/"; 
  } 
 
  int loginUserId = loginUser.getUserId(); 
  //此處的productService是注入的IProductService接口的對(duì)象 
  this.productService.showProductsByPage(request, model, loginUserId); 
 
  return "跳轉(zhuǎn)到的JSP路徑"; 
}

JSP頁(yè)面接受部分我就不寫了,每個(gè)人都一樣,也就是結(jié)合JSTL和EL來寫,(在循環(huán)輸出的時(shí)候也做了判斷,如果接受的參數(shù)為空,那么輸出暫無商品,只有接受的參數(shù)不為空的時(shí)候,才循環(huán)輸出,使用<<c:when test="${}">結(jié)合<c:otherwise>),這里只給出分頁(yè)的相關(guān)代碼:

<!-- 分頁(yè)功能 start --> 
  <div align="center"> 
    <font size="2">共 ${page.totalPageCount} 頁(yè)</font> <font size="2">第 
      ${page.pageNow} 頁(yè)</font> <a href="myProductPage?pageNow=1" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> 
    <c:choose> 
      <c:when test="${page.pageNow - 1 > 0}"> 
        <a href="myProductPage?pageNow=${page.pageNow - 1}" rel="external nofollow" >上一頁(yè)</a> 
      </c:when> 
      <c:when test="${page.pageNow - 1 <= 0}"> 
        <a href="myProductPage?pageNow=1" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a> 
      </c:when> 
    </c:choose> 
    <c:choose> 
      <c:when test="${page.totalPageCount==0}"> 
        <a href="myProductPage?pageNow=${page.pageNow}" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a> 
      </c:when> 
      <c:when test="${page.pageNow + 1 < page.totalPageCount}"> 
        <a href="myProductPage?pageNow=${page.pageNow + 1}" rel="external nofollow" >下一頁(yè)</a> 
      </c:when> 
      <c:when test="${page.pageNow + 1 >= page.totalPageCount}"> 
        <a href="myProductPage?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a> 
      </c:when> 
    </c:choose> 
    <c:choose> 
      <c:when test="${page.totalPageCount==0}"> 
        <a href="myProductPage?pageNow=${page.pageNow}" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a> 
      </c:when> 
      <c:otherwise> 
        <a href="myProductPage?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a> 
      </c:otherwise> 
    </c:choose> 
  </div> 
  <!-- 分頁(yè)功能 End -->

2.查詢分頁(yè)

關(guān)于查詢分頁(yè),大致過程完全一樣,只是第三個(gè)參數(shù)(上面是loginUserId)需要接受用戶輸入的參數(shù),這樣的話我們需要在控制器中接受用戶輸入的這個(gè)參數(shù)(頁(yè)面中的<input>使用GET方式傳參),然后將其加入到SESSION中,即可完成查詢分頁(yè)(此處由于“下一頁(yè)”這中超鏈接的原因,使用了不同的JSP頁(yè)面處理分頁(yè)和搜索分頁(yè),暫時(shí)沒找到在一個(gè)JSP頁(yè)面中完成的方法,出現(xiàn)了重復(fù)代碼,這里的重復(fù)代碼就是輸出內(nèi)容的那段代碼,可以單獨(dú)拿出去,然后用一個(gè)<include>標(biāo)簽加載到需要的JSP頁(yè)面就可以了,這樣可以避免代碼重復(fù)):

這里給出控制器的代碼作為參考:

/** 
  * 通過 產(chǎn)品名稱 查詢產(chǎn)品 
  * @param request 
  * @param model 
  * @return 
  */ 
  @RequestMapping(value = "映射地址", method = RequestMethod.GET) 
  public String searchForProducts(HttpServletRequest request, Model model) { 
    HttpSession session = request.getSession(); 
 
    String param = request.getParameter("param"); 
 
    String condition = (String) session.getAttribute("condition"); 
 
    //先判斷SESSION中的condition是否為空 
    if (condition == null) { 
      condition = new String(); 
      session.setAttribute("condition", condition); 
      //如果Session中的condition為空,再判斷傳入的參數(shù)是否為空,如果為空就跳轉(zhuǎn)到搜索結(jié)果頁(yè)面 
      if (param == null || "".equals(param)) { 
        return "private/space/ProductSearchResult"; 
      } 
    } 
    //如果SESSION不為空,且傳入的搜索條件param不為空,那么將param賦值給condition 
    if (param != null && !("".equals(param))) { 
      condition = param; 
      session.setAttribute("condition", condition); 
    } 
    //使用session中的condition屬性值來作為查詢條件 
    this.productService.showSearchedProductsByPage(request, model, condition); 
 
    return "跳轉(zhuǎn)的頁(yè)面"; 
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SSH框架網(wǎng)上商城項(xiàng)目第16戰(zhàn)之Hibernate二級(jí)緩存處理首頁(yè)熱門顯示

    SSH框架網(wǎng)上商城項(xiàng)目第16戰(zhàn)之Hibernate二級(jí)緩存處理首頁(yè)熱門顯示

    這篇文章主要介紹了SSH框架網(wǎng)上商城項(xiàng)目第16戰(zhàn)之Hibernate的二級(jí)緩存處理首頁(yè)的熱門顯示,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 在Spring中實(shí)現(xiàn)異步處理的步驟和代碼演示

    在Spring中實(shí)現(xiàn)異步處理的步驟和代碼演示

    在Spring中實(shí)現(xiàn)異步處理通常涉及到@Async注解,通過步驟和代碼演示,可以在Spring應(yīng)用程序中實(shí)現(xiàn)異步處理,記住要根據(jù)你的應(yīng)用程序的實(shí)際需求來調(diào)整線程池和異步方法的設(shè)計(jì),感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • java反射耗時(shí)測(cè)試案例解析

    java反射耗時(shí)測(cè)試案例解析

    這篇文章主要介紹了java反射耗時(shí)測(cè)試案例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java IO流 文件傳輸基礎(chǔ)

    Java IO流 文件傳輸基礎(chǔ)

    這篇文章主要介紹了Java IO流 文件傳輸基礎(chǔ)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情

    SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情

    這篇文章主要介紹了SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Idea中springboot項(xiàng)目的熱部署無法生效問題解決

    Idea中springboot項(xiàng)目的熱部署無法生效問題解決

    本文主要介紹了Idea中springboot項(xiàng)目的熱部署無法生效問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-10-10
  • Mybatis一對(duì)多與多對(duì)一查詢處理詳解

    Mybatis一對(duì)多與多對(duì)一查詢處理詳解

    這篇文章主要給大家介紹了關(guān)于Mybatis一對(duì)多與多對(duì)一查詢處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java實(shí)現(xiàn)用Freemarker完美導(dǎo)出word文檔(帶圖片)

    Java實(shí)現(xiàn)用Freemarker完美導(dǎo)出word文檔(帶圖片)

    這篇文章主要介紹了Java實(shí)現(xiàn)用Freemarker完美導(dǎo)出word文檔(帶圖片),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 詳細(xì)分析JAVA加解密算法

    詳細(xì)分析JAVA加解密算法

    這篇文章主要介紹了JAVA加解密算法的的相關(guān)資料,文中講解非常詳細(xì),代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Java將byte[]轉(zhuǎn)圖片存儲(chǔ)到本地的案例

    Java將byte[]轉(zhuǎn)圖片存儲(chǔ)到本地的案例

    這篇文章主要介紹了Java將byte[]轉(zhuǎn)圖片存儲(chǔ)到本地的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評(píng)論