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

Spring Hibernate實(shí)現(xiàn)分頁(yè)功能

 更新時(shí)間:2017年05月05日 09:32:12   作者:Coder_py  
這篇文章主要為大家詳細(xì)介紹了Spring Hibernate實(shí)現(xiàn)分頁(yè)功能的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本實(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ì)象開始查詢,參數(shù)的索引位置是從0開始的。

query.setMaxResults(p.getRows()); 分頁(yè)時(shí),一次最多產(chǎn)尋的對(duì)象數(shù) 主要實(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頁(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)文章

最新評(píng)論

连城县| 电白县| 宁津县| 呼伦贝尔市| 龙岩市| 灵川县| 石棉县| 山阴县| 古田县| 庆元县| 涪陵区| 西青区| 淄博市| 青冈县| 张北县| 公安县| 长兴县| 江都市| 五常市| 肇庆市| 阳春市| 禄丰县| 当雄县| 楚雄市| 天等县| 安宁市| 上饶县| 轮台县| 神农架林区| 通山县| 绥中县| 曲松县| 青浦区| 镇原县| 白银市| 信宜市| 克什克腾旗| 嘉禾县| 永登县| 故城县| 宁陵县|