Spring Hibernate實(shí)現(xiàn)分頁(yè)功能
本實(shí)例采用Spring+Hibernate實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)功能,供大家參考,具體內(nèi)容如下
最關(guān)鍵的是運(yùn)用Hibernate的query里面的兩個(gè)方法:
query.setFirstResult((p.getPage()-1)*p.getRows()); 指定從那個(gè)對(duì)象開(kāi)始查詢,參數(shù)的索引位置是從0開(kāi)始的。
query.setMaxResults(p.getRows()); 分頁(yè)時(shí),一次最多產(chǎn)尋的對(duì)象數(shù) 主要實(shí)現(xiàn)類(lèi):
package com.paging; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.SessionFactory; import com.user.User; import sun.nio.cs.US_ASCII; public class Paging { final int num=3; @Resource SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public List<User> paging(int index) { String hql = "from User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setFirstResult((index-1)*num); query.setMaxResults(num); return query.list(); } }
web層:
package com.web; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.paging.Paging; import com.user.User; @Controller @RequestMapping("/Page") public class Web { @Resource Paging paging; public void setPaging(Paging paging) { this.paging = paging; } @RequestMapping("/page") public String page(Model model,int index) { List<User> list = paging.paging(index); model.addAttribute("list", list); return "index"; } }
jsp頁(yè)面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <body> <h1><a href="/Paging/Page/page?index=1" rel="external nofollow" >1</a></h1> <h1><a href="/Paging/Page/page?index=2" rel="external nofollow" >2</a></h1> <h1><a href="/Paging/Page/page?index=3" rel="external nofollow" >3</a></h1> <c:if test="${!empty list }"> <c:forEach items="${list}" var="list"> ${list.name} ${list.adderss} </c:forEach> </c:if> </body> </html>
因?yàn)槭呛?jiǎn)單例子所以界面就很簡(jiǎn)陋了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java單例模式利用HashMap實(shí)現(xiàn)緩存數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java單例模式利用HashMap實(shí)現(xiàn)緩存數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Java Agent入門(mén)學(xué)習(xí)之動(dòng)態(tài)修改代碼
這篇文章主要給大家分享了Java Agent入門(mén)學(xué)習(xí)之動(dòng)態(tài)修改代碼的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07Java 內(nèi)存模型中的happen-before關(guān)系詳解
這篇文章主要為大家介紹了Java 內(nèi)存模型中的happen-before關(guān)系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10SpringBoot接收請(qǐng)求參數(shù)的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于SpringBoot接收請(qǐng)求參數(shù)的四種方式,文中通過(guò)代碼以及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SpringBoot具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09SpringBoot集成Nacos實(shí)現(xiàn)注冊(cè)中心與配置中心流程詳解
這篇文章主要介紹了SpringBoot集成Nacos實(shí)現(xiàn)注冊(cè)中心與配置中心流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02