hibernate3.4+struts1.3分页封装,有兴趣者可以看一下
2019独角兽企业重金招聘Python工程师标准>>>
Hibernate3.4+struts1.3分页的封装
环境:hibernate3.4 +struts1.3 +jsp
Page.java
package com.nongxue.util;
public class Page {
private int totalPage;//总页数
private int curPage;//当前页数
public static int maxResult = 2;//每页的最大记录条数
public Page(int toPage,int curPage){
this.setTotalPage(toPage);
this.setCurPage(curPage);
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
if(totalPage == 0){
this.totalPage = 1;
return;
}
this.totalPage = totalPage%maxResult == 0?totalPage/maxResult : totalPage/maxResult+1;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
}
QueryResult.java
package com.nongxue.util;
import java.util.List;
public class QueryResult<T> {
private List<T> records;
private Page page;
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}
查询总记录数的代码
public int getArticleAccount(StringBuffer HQL)
{
Session session = new ComonUtil().getSession();
org.hibernate.Transaction tr = session.beginTransaction();
int temp=0;
try {
String Hquery = "from Article"+HQL;
Query query = session.createQuery(Hquery);
temp =query.list().size();
System.out.println("ddddddddddd"+temp);
tr.commit();
} catch (HibernateException e) {
tr.rollback();
e.printStackTrace();
} finally{
session.close();
}
return temp;
}
分页查询代码
public QueryResult<Article> getAllArticle(int page,StringBuffer HQL) {
List<Article> list = new ArrayList<Article>();
Session session =new ComonUtil().getSession();
org.hibernate.Transaction tr = session.beginTransaction();
QueryResult<Article> queryResult = null;
try {
Page pae = new Page(this.getArticleAccount(HQL),page);
System.out.println("==hql=="+HQL);
String hquery="from Article"+HQL+"order by articleId desc";
System.out.println("=======HQL2======"+hquery);
Query query = session.createQuery(hquery);
query.setFirstResult(Page.maxResult*(page-1));
query.setMaxResults(Page.maxResult);
list = query.list();
queryResult = new QueryResult<Article>();
queryResult.setPage(pae);
queryResult.setRecords(list);
} catch (HibernateException e) {
tr.rollback();
e.printStackTrace();
}
return queryResult;
}
其中ComonUtil().getSession()调用的代码是
public class ComonUtil {
//拿到session
public Session getSession()
{
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
return session;
}
……
}
Action中调用
public ActionForward getAllArticleByPage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String forwardName = null;
int page = 0;
try{
page = Integer.parseInt(request.getParameter("page"));
}catch(Exception e){
page = 1;
}
QueryResult<Article> articleList = null;
int stutus =0;
StringBuffer HQL = new StringBuffer();
//判断是否有状态值
try {
stutus = Integer.parseInt(request.getParameter("stutus"));
try {
Student student = (Student) request.getSession().getAttribute("student");
String sname = student.getStudentTrueName();
System.out.println("ddddddddddddd "+sname);
articleList = CommServce.getInstence().getAllArticle(page,HQL.append(" where articleUser='"+sname+"' "));
forwardName="allarticle";
} catch (Exception e) {
articleList = CommServce.getInstence().getAllArticle(page,HQL.append(" where stutus="+stutus+" "));
forwardName="articleList";
}
} catch (Exception e) {
Student student = (Student) request.getSession().getAttribute("student");
String sname = student.getStudentTrueName();
System.out.println("ddddddddddddd "+sname);
articleList = CommServce.getInstence().getAllArticle(page,HQL.append(" where articleUser='"+sname+"' "));
forwardName="allarticle";
}
request.setAttribute("article", articleList);
request.setAttribute("curPage", page);
return mapping.findForward(forwardName);
}
页面jsp上的显示
<center>
<a href="ArticleAction.do?comm=getAllArticleByPage&&page=1">首页</a>
<c:if test="${curPage==1}">
上一页
</c:if>
<c:if test="${curPage>1}">
<a href="ArticleAction.do?comm=getAllArticleByPage&&page=${article.page.curPage-1}">上一页</a>
</c:if>
<c:if test="${curPage==article.page.totalPage}">
下一页
</c:if>
<c:if test="${curPage<article.page.totalPage}">
<a href="ArticleAction.do?comm=getAllArticleByPage&&page=${article.page.curPage+1}">下一页</a>
</c:if>
<a href="ArticleAction.do?comm=getAllArticleByPage&&page=${article.page.totalPage}">末页</a>
共<font color="red">${article.page.totalPage}</font>页
</center>
这是一个非常通用 的分页方法,各位可以进行拓展,如果有不了解的地方也可以联系我:QQ443628678
转载于:https://my.oschina.net/lushuifa/blog/1545958
hibernate3.4+struts1.3分页封装,有兴趣者可以看一下相关推荐
- 分页封装实用工具类及其使用方法
分页封装实用工具类及其使用方法 作者: javaboy2012 Email:yanek@163.com qq: 1046011462 package com.yanek.util; import ...
- js f12就关闭标签、禁止右击,感兴趣的可以看一下
翻了又一个网页,f12就关闭标签.禁止右击,有点意思..感兴趣的可以看一下. <script>function fuckyou(){ window.close(); //关闭当前窗口(防抽 ...
- 小程序开发(12)-之分页封装
这里的分页呢就记录下,上拉加载的,小程序给我们提供了一个监听滚动到底部的周期函数onReachBottom,当页面超过整屏的高度后,滚动页面到底部就会触发这个事件,所以做上拉加载的时候,非常的方便,下 ...
- java分页封装到dao层,Node Dao层的封装与分页
传统的写法/** * Created by 黄森 on 2017/7/22. */var express = require('express');var app = express();var Mo ...
- 原生js实现动态表格分页(封装版)
之前一直用的插件做表格分页,但是不同的场景用到的情况都不同,所以索性用个原生js搞一个封装好的. 后台动态获取数据进行渲染,等页面渲染成功之后在进行分页.数据的获取我用的vue,也可以用ajax等其他 ...
- element 表格+分页封装
原因:element ui 的表格组件默认是不带分页的功能,但是实际工作中表格总是搭配分页功能一起出现的.封装在一起使用方便不需要每次都复制粘贴一大堆. 可以根据自己的需求更改,自己根据设计图定义样式 ...
- JS表格分页(封装版)
HTML代码: <html> <head><meta charset='utf-8'> <script type="text/javascript& ...
- 基于vue的element-ui中无总页数的分页封装
借鉴于https://blog.csdn.net/NirvanaRebirth/article/details/92412794,感谢! 上面介绍的很详细,同时在此基础上进行了样式上的调整,样式写的比 ...
- 分页封装 PageInfo
MAVEM <dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehe ...
最新文章
- 小型职工工作量c语言,计算机c语言职工工作量统计系统(20页)-原创力文档
- python可以做什么 知乎-python能做什么知乎
- 400+节点的 Elasticsearch 集群运维
- 数据结构---二叉搜索树
- Linux+php+memcache+APC加速PHP网站
- 2015年《大数据》高被引论文Top10文章No.7——大数据机器学习系统研究进展(上)...
- 红橙Darren视频笔记 builder设计模式 navigationbar 导航栏第二版
- 计算包含+、-、*、/、(、)等几种运算符的表达式的值。
- mysql select 返回列_mySQL – 使用select返回多行来更新多个列
- java flag 用法_Java中一些常用的方法
- 劳埃镜 matlab,光学习题及答案
- 如果不懂 numpy,请别说自己是 python 程序员
- Python 路径操作
- 【学习笔记】山东大学生物信息学-05 高通量测序技术介绍 + 06 统计基础与序列算法(原理)
- Android-NFC模块学习(1)
- 如何测试一只一支圆珠笔
- cmd 组合命令和管道命令的使用
- 【C语言】分支语句和循环语句还能这么用?一篇带你重新认识它(两万字超细详解)
- OPPO Reno2抢跑视频新赛道,能打破5G的“无极混沌”吗?
- 记录--vue+three,制作iview大波浪特效