Spring Hibernate實現分頁功能
更新時間:2017年05月05日 09:32:12 作者:Coder_py
這篇文章主要為大家詳細介紹了Spring Hibernate實現分頁功能的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本實例采用Spring+Hibernate實現簡單的分頁功能,供大家參考,具體內容如下
最關鍵的是運用Hibernate的query里面的兩個方法:
query.setFirstResult((p.getPage()-1)*p.getRows()); 指定從那個對象開始查詢,參數的索引位置是從0開始的。
query.setMaxResults(p.getRows()); 分頁時,一次最多產尋的對象數 主要實現類:
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>
因為是簡單例子所以界面就很簡陋了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java WebSocket的實現以及Spring WebSocket示例代碼
本篇文章主要介紹了java WebSocket的實現以及Spring WebSocket,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
druid?handleException執(zhí)行流程源碼解析
這篇文章主要為大家介紹了druid?handleException執(zhí)行流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09

