Hibernate分页

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;

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值