Spring Hibernate實現(xiàn)分頁功能
本實例采用Spring+Hibernate實現(xiàn)簡單的分頁功能,供大家參考,具體內(nèi)容如下
最關(guān)鍵的是運用Hibernate的query里面的兩個方法:
query.setFirstResult((p.getPage()-1)*p.getRows()); 指定從那個對象開始查詢,參數(shù)的索引位置是從0開始的。
query.setMaxResults(p.getRows()); 分頁時,一次最多產(chǎn)尋的對象數(shù) 主要實現(xiàn)類:
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頁面:
<%@ 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>
因為是簡單例子所以界面就很簡陋了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java單例模式利用HashMap實現(xiàn)緩存數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java單例模式利用HashMap實現(xiàn)緩存數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Java Agent入門學(xué)習(xí)之動態(tài)修改代碼
這篇文章主要給大家分享了Java Agent入門學(xué)習(xí)之動態(tài)修改代碼的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-07-07
Java 內(nèi)存模型中的happen-before關(guān)系詳解
這篇文章主要為大家介紹了Java 內(nèi)存模型中的happen-before關(guān)系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
SpringBoot接收請求參數(shù)的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于SpringBoot接收請求參數(shù)的四種方式,文中通過代碼以及圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SpringBoot具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
SpringBoot集成Nacos實現(xiàn)注冊中心與配置中心流程詳解
這篇文章主要介紹了SpringBoot集成Nacos實現(xiàn)注冊中心與配置中心流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02

