DAO类
package com.coolpep.order.dao;
import java.util.List;
public interface PaginationDAO {
public int getCount(String cond) throws Exception;
public int getPage(String cond) throws Exception;
public List getPage(String cond, int page, int cont) throws Exception;
}
DAPImpl类
package com.coolpep.order.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.coolpep.order.dao.PaginationDAO;
import com.coolpep.order.factory.DAOFactory;
public class PaginationDAOImpl implements PaginationDAO {
private Session session = null;
private int totalPage; //总页数
private int totalCount; //总行数
private int pageSize = 5; //页面显示条数
private int currentPage; //当前页码
public PaginationDAOImpl() {
// TODO Auto-generated constructor stub
this.session = new Configuration().configure().buildSessionFactory().openSession();
}
/**
* 汇总数据获取数据总行数
*
* */
public int getCount(String cond) throws Exception{
int totalCount = 0;
try {
Transaction tran = this.session.beginTransaction();
Query q = this.session.createQuery("select count(*) from Orders where osource=?");
q.setString(0, cond);
List list = q.list();
totalCount = ((Integer) list.get(0)).intValue();
tran.commit();
this.session.close();
} catch (Exception e) {
e.printStackTrace();
}
return totalCount;
}
/**
* 汇总数据获取数据总页数
*
* */
public int getPage(String cond) throws Exception{
try{
Transaction tran = this.session.beginTransaction();
DAOFactory dao = new DAOFactory();
totalCount = dao.getPaginationDAOInstance().getCount(cond);
if(totalCount%pageSize>0)
totalPage = totalCount/pageSize + 1;
if(totalCount%pageSize==0)
totalPage = totalCount/pageSize;
if(totalCount%pageSize<0)
totalPage = totalCount/pageSize;
tran.commit();
this.session.close();
} catch (Exception e){
e.printStackTrace();
}
return totalPage;
}
/**
* 根据条件分页
*
* @param cond 查找条件
* @param page 当前页数
* @param count
* */
public List getPage(String cond, int currentPage, int pageSize){
List list = null;
String hql = "from Orders where osource=?";
Query q = this.session.createQuery(hql);
q.setString(0, cond);
q.setFirstResult(pageSize*(currentPage-1));
q.setMaxResults(pageSize);
list = q.list();
return list;
}
}
分享到:
相关推荐
Hibernate分页查询小结
hibernate分页Hibernate 分页的设计和编码
hibernate分页查询 数据库连接,不错的分析,可以尝试下载易用。。
Hibernate分页查询原理解读 , 忘记以前在哪看的了, 现在贴出来和大家分享一下。
hibernate分页(无排序,搜索,仅仅分页显示),服务器端分页在datatables上展现,有关 datatables的知识请关注它的官网http://www.datatables.net/,datatables的功能很 全面。 2,建表的sql--studentinfo和插入...
STRUTS2+HIBERNATE分页 实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码
hibernate分页查询,里面包含数据库。自己写的。能正常运行。
传统的网页设计只在一个JSP或者ASP页面中书写所有关于数据库操作的代码,那样做分页可能简单一点,但当把网站分层开发后,分页就比较困难了,下面是我做Spring+Hibernate+Struts2项目时设计的分页代码,与大家分享...
hibernate分页代码,直接能在myeclipse上运行的好东西
spring+hibernate 分页 +mysql 可以当做模板用。
hibernate分页 博文链接:https://iomo.iteye.com/blog/243518
java 实现的一个简单的hibernate分页类 可以设置,从某一条开始取、显示的条数 不依赖struts spring
hibernate分页
Struts+Hibernate分页标签 Struts+Hibernate分页标签
Hibernate分页教学视频 Hibernate分页教学视频 Hibernate分页教学视频
这是一个类,可以通用,只要传入两个参数即可,一个是...Query query是hibernate中session创建的query;字符char ch代表操作:‘a’就是all全部,'n'就是next下一页,'u'就是up上一页;'e'就是end末页,'s'就是start首页
Struts2+Sprint+Hibernate分页查询功能,实现过程步骤完整。
Hibernate分页可能引起的性能问题 在使用Hibernate进行分页的过程中,如果你收到如下警告,那么这里就是一个潜在的性能问题点: WARNING: firstResult/maxResults specified with collection fetch; applying in ...