最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringData JPA實(shí)現(xiàn)查詢分頁demo

 更新時(shí)間:2017年03月01日 10:06:52   作者:夢中彩虹  
本篇文章主要介紹了SpringData JPA實(shí)現(xiàn)查詢分頁demo,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

SpringData JPA 的 PagingAndSortingRepository接口已經(jīng)提供了對分頁的支持,查詢的時(shí)候我們只需要傳入一個(gè) org.springframework.data.domain.Pageable

接口的實(shí)現(xiàn)類,指定PageNumber和pageSize即可

springData包中的 PageRequest類已經(jīng)實(shí)現(xiàn)了Pageable接口,我們可以直接使用下邊是部分代碼:

DAO:

package com.jiaoyiping.jdjy.sourcecode.dao;

import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode;
import org.springframework.data.repository.PagingAndSortingRepository;

/**
 * Created with IntelliJ IDEA.
 * User: 焦一平
 * Date: 14-11-20
 * Time: 下午11:18
 * To change this template use File | Settings | File Templates.
 */
public interface SourceCodeDao extends PagingAndSortingRepository<SourceCode, String> {

}

service:

package com.jiaoyiping.jdjy.sourcecode.service;

import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode;
import com.jiaoyiping.jdjy.sourcecode.dao.SourceCodeDao;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.transaction.Transactional;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 焦一平
 * Date: 14-11-20
 * Time: 下午11:24
 * To change this template use File | Settings | File Templates.
 */
public class SourceCodeService {
  @Autowired
  private SourceCodeDao sourceCodeDao;public Page<SourceCode> getSourceCode(int pageNumber,int pageSize){
    PageRequest request = this.buildPageRequest(pageNumber,pageSize);
    Page<SourceCode> sourceCodes= this.sourceCodeDao.findAll(request);
    return sourceCodes;
  }
  //構(gòu)建PageRequest
  private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
    return new PageRequest(pageNumber - 1, pagzSize, null);
  }

}

controller:

package com.jiaoyiping.jdjy.sourcecode.controller;
import com.jiaoyiping.jdjy.sourcecode.Const;
import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode;
import com.jiaoyiping.jdjy.sourcecode.service.SourceCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: 焦一平
 * Date: 14-11-20
 * Time: 下午11:22
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/sourcecode")
public class SourceCodeController {
  @Autowired
  private SourceCodeService sourceCodeService;

  
  @RequestMapping(value = "list")
  public ModelAndView listSourceCode(HttpServletRequest request, HttpServletResponse response){
    String pageNumberStr=request.getParameter("pageNumber");
    if(pageNumberStr==null ||"".equals(pageNumberStr)){
      pageNumberStr="1";
    }
    int pageNumber = Integer.parseInt(pageNumberStr);
    int pageSize = Const.PAGE_SIZE;
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("/sourcecode/listSourceCode");
    Page<SourceCode> sourceCodes = this.sourceCodeService.getSourceCode(pageNumber, pageSize);
    modelAndView.addObject("sourceCodeList",sourceCodes.getContent());
    modelAndView.addObject("totalPageNumber",sourceCodes.getTotalElements());
    modelAndView.addObject("pageSize",pageSize);
    return modelAndView;

  }

}

 前端分頁:

前端分頁組件我們使用bootstrap提供的分頁組件:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%--
 Created by IntelliJ IDEA.
 User: 焦一平
 Date: 2014/12/27
 Time: 9:57
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String basePath = request.getContextPath();
 String MethodURL=basePath+"/sourcecode/list.action?pageNumber=";
%>
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8"/>
 <title>源代碼列表</title>

 <link href="<%=basePath%>/resources/assets/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"/>
 <script type="text/javascript" src="<%=basePath%>/resources/js/jquery/jquery.js"></script>


 <script type="text/javascript">
  $(document).ready(function(){
   var totalNumber = Number(${totalPageNumber});
   var pageSize = Number(${pageSize});
   var pageCount = totalNumber/pageSize;
   var html = "";
   for(var i = 0;i<pageCount;i++){
    var link_Url = "<li><a href=\"<%=MethodURL%>"+(i+1)+"\">"+(i+1)+"</a></li>";
    html += link_Url;
   }
   var fenyeDiv = document.getElementById("link");
   fenyeDiv.innerHTML=html;
  });
 </script>
</head>
<body>
<a href="#" rel="external nofollow" class="list-group-item active">
 源代碼列表
</a>
  <c:forEach items="${sourceCodeList}" var="sourceCode">
   <a href="<%=request.getContextPath()%>/sourcecode/detail.action?id=<c:out value=" rel="external nofollow" ${sourceCode.id}" />" class="list-group-item"><c:out value="${sourceCode.title}" /></a>
  </c:forEach>
<!-- 列表分頁的DIV,由JS動(dòng)態(tài)填充內(nèi)容-->
<ul class="pagination pagination-lg" id="link">

</ul><br>

</body>
</html> 

最終結(jié)果如下:

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

相關(guān)文章

  • SpringIOC容器Bean的作用域及生命周期實(shí)例

    SpringIOC容器Bean的作用域及生命周期實(shí)例

    這篇文章主要為大家介紹了SpringIOC容器Bean的作用域及生命周期實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • springBoot集成redis的key,value序列化的相關(guān)問題

    springBoot集成redis的key,value序列化的相關(guān)問題

    這篇文章主要介紹了springBoot集成redis的key,value序列化的相關(guān)問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • java裁剪圖片并保存的示例分享

    java裁剪圖片并保存的示例分享

    在這篇文章中我們將學(xué)習(xí)如何用Java 對圖像進(jìn)行剪裁并將剪裁出來的部分單獨(dú)保存到文件中
    2014-01-01
  • Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件的方法

    Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件的方法,結(jié)合實(shí)例形式分析了Java調(diào)用jython執(zhí)行python文件的常見操作技巧及相關(guān)問題解決方法,需要的朋友可以參考下
    2018-03-03
  • Java中Hashtable集合的常用方法詳解

    Java中Hashtable集合的常用方法詳解

    本篇文章給大家?guī)淼膬?nèi)容是關(guān)于Java中Hashtable集合的常用方法詳解,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對你有所幫助。下面我們就來學(xué)習(xí)一下吧
    2021-11-11
  • Redis六大數(shù)據(jù)類型使用方法詳解

    Redis六大數(shù)據(jù)類型使用方法詳解

    這篇文章主要介紹了Redis六大數(shù)據(jù)類型使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能

    java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • spring狀態(tài)機(jī)模式使用小結(jié)

    spring狀態(tài)機(jī)模式使用小結(jié)

    說起Spring狀態(tài)機(jī),大家很容易聯(lián)想到這個(gè)狀態(tài)機(jī)和設(shè)計(jì)模式中狀態(tài)模式的區(qū)別是啥呢?沒錯(cuò),Spring狀態(tài)機(jī)就是狀態(tài)模式的一種實(shí)現(xiàn),在介紹Spring狀態(tài)機(jī)之前,讓我們來看看設(shè)計(jì)模式中的狀態(tài)模式,需要的朋友可以參考下
    2024-04-04
  • 自定義application.yml配置項(xiàng)方式

    自定義application.yml配置項(xiàng)方式

    這篇文章主要介紹了自定義application.yml配置項(xiàng)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲

    基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲

    這篇文章主要介紹了基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2020-10-10

最新評論

弥勒县| 垣曲县| 克东县| 手游| 茶陵县| 奉节县| 南澳县| 虞城县| 舒城县| 桓仁| 台南县| 镇原县| 康马县| 楚雄市| 永兴县| 洪泽县| 桓台县| 南溪县| 武陟县| 富川| 扎鲁特旗| 辛集市| 深州市| 博客| 从江县| 界首市| 泊头市| 漠河县| 宁化县| 洞头县| 鲜城| 安新县| 钦州市| 彩票| 长沙县| 天峻县| 克拉玛依市| 徐州市| 永川市| 丰原市| 班玛县|