Page.java
- import java.util.List;
- /**
- * 分页Page类
- * @author zhxing
- *
- * @param <T>
- */
- public class Page<T>{
- public final int DEFAULT_PAGESIZE= 10; // 每页记录数
- private List<T> result = null;//页面数据
- private int totalRows;//总记录数
- private int pageSize=DEFAULT_PAGESIZE;//每页显示行数
- private int currentPage;//当前页数
- private int totalPages;//总页数
- private int startRow;//查询开始的记录数
- public Page(){
- }
- public Page(int totalRows){
- //初始化总记录数
- this.totalRows=totalRows;
- //初始化总页数
- this.totalPages=totalRows/pageSize;
- if(totalRows%pageSize!=0){
- totalPages++;
- }
- //初始化当前页数
- this.currentPage=1;
- //初始化查询开始的记录数
- this.startRow=0;
- }
- // 页内的数据列表.
- public List<T> getResult() {
- return result;
- }
- public void setResult(List<T> result) {
- this.result = result;
- }
- // 获得上一页
- public void previous(){
- if(currentPage==1)
- return;
- currentPage--;
- startRow=(currentPage-1)*pageSize;
- }
- //获得下一页
- public void next(){
- if(currentPage==totalPages){
- return;
- }
- currentPage++;
- startRow=(currentPage+1)*pageSize;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- //设置当前页
- public void setCurrentPage(int currentPage) {
- if(currentPage>=totalPages){
- if(totalPages==0)
- this.currentPage=1;
- else
- this.currentPage=totalPages;
- }
- if(1<currentPage&¤tPage<totalPages){
- this.currentPage=currentPage;
- }
- if(currentPage<1){
- this.currentPage=1;
- }
- }
- public int getStartRow() {
- startRow=(currentPage-1)*pageSize;
- return startRow;
- }
- public void setStartRow(int startRow) {
- this.startRow = startRow;
- }
- public int getTotalRows() {
- return totalRows;
- }
- public void setTotalRows(int totalRows) {
- this.totalRows = totalRows;
- }
- public int getTotalPages() {
- return totalPages;
- }
- public int getPageSize() {
- return pageSize;
- }
- public boolean isStart() {
- return currentPage==1;
- }
- public boolean isEnd() {
- return currentPage==totalPages||totalPages==0;
- }
- }
import java.util.List;
/**
* 分页Page类
* @author zhxing
*
* @param <T>
*/
public class Page<T>{
public final int DEFAULT_PAGESIZE= 10; // 每页记录数
private List<T> result = null;//页面数据
private int totalRows;//总记录数
private int pageSize=DEFAULT_PAGESIZE;//每页显示行数
private int currentPage;//当前页数
private int totalPages;//总页数
private int startRow;//查询开始的记录数
public Page(){
}
public Page(int totalRows){
//初始化总记录数
this.totalRows=totalRows;
//初始化总页数
this.totalPages=totalRows/pageSize;
if(totalRows%pageSize!=0){
totalPages++;
}
//初始化当前页数
this.currentPage=1;
//初始化查询开始的记录数
this.startRow=0;
}
// 页内的数据列表.
public List<T> getResult() {
return result;
}
public void setResult(List<T> result) {
this.result = result;
}
// 获得上一页
public void previous(){
if(currentPage==1)
return;
currentPage--;
startRow=(currentPage-1)*pageSize;
}
//获得下一页
public void next(){
if(currentPage==totalPages){
return;
}
currentPage++;
startRow=(currentPage+1)*pageSize;
}
public int getCurrentPage() {
return currentPage;
}
//设置当前页
public void setCurrentPage(int currentPage) {
if(currentPage>=totalPages){
if(totalPages==0)
this.currentPage=1;
else
this.currentPage=totalPages;
}
if(1<currentPage&¤tPage<totalPages){
this.currentPage=currentPage;
}
if(currentPage<1){
this.currentPage=1;
}
}
public int getStartRow() {
startRow=(currentPage-1)*pageSize;
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getTotalPages() {
return totalPages;
}
public int getPageSize() {
return pageSize;
}
public boolean isStart() {
return currentPage==1;
}
public boolean isEnd() {
return currentPage==totalPages||totalPages==0;
}
}
BaseDao.java
- import java.io.Serializable;
- import java.util.List;
- public interface BaseDao<T,ID extends Serializable> {
- /**
- * 保存实体
- * @param entity 实体类
- */
- public void save(T entity);
- /**
- * 删除实体
- * @param entity 实体类
- */
- public void delete(T entity);
- /**
- * 根据实体id 删除实体
- * @param entityClass 实体类
- * @param id 实体id
- */
- public void deleteById(Class<T> entityClass,ID id);
- /**
- * 更新实体
- * @param entity 实体类
- */
- public void update(T entity);
- /**
- * 根据实体id 查询单个实体
- * @param entityClass 实体类
- * @param id 实体id
- * @return
- */
- public T findById(Class<T> entityClass,ID id);
- /**
- * 列出所有实体集合
- * @param entityClass 实体类
- * @return 实体类List
- */
- public List<T> findAll(Class<T> entityClass);
- /**
- * 根据实体参数,查询符合条件的实体类集合
- * @param hql
- * @param values 参数
- * @return
- */
- public List<Object> find(String hql, Object... values);
- /**
- * 根据hql 语句,返回Page 类
- * @param page Page类
- * @param hql @param hql
- * @param currentPage 当前页码
- * @return
- */
- public Page<T> findByPage(final String hql,final String countHql,final int currentPage);
- }
import java.io.Serializable;
import java.util.List;
public interface BaseDao<T,ID extends Serializable> {
/**
* 保存实体
* @param entity 实体类
*/
public void save(T entity);
/**
* 删除实体
* @param entity 实体类
*/
public void delete(T entity);
/**
* 根据实体id 删除实体
* @param entityClass 实体类
* @param id 实体id
*/
public void deleteById(Class<T> entityClass,ID id);
/**
* 更新实体
* @param entity 实体类
*/
public void update(T entity);
/**
* 根据实体id 查询单个实体
* @param entityClass 实体类
* @param id 实体id
* @return
*/
public T findById(Class<T> entityClass,ID id);
/**
* 列出所有实体集合
* @param entityClass 实体类
* @return 实体类List
*/
public List<T> findAll(Class<T> entityClass);
/**
* 根据实体参数,查询符合条件的实体类集合
* @param hql
* @param values 参数
* @return
*/
public List<Object> find(String hql, Object... values);
/**
* 根据hql 语句,返回Page 类
* @param page Page类
* @param hql @param hql
* @param currentPage 当前页码
* @return
*/
public Page<T> findByPage(final String hql,final String countHql,final int currentPage);
}
BaseHibernateDao.java
- import java.io.Serializable;
- import java.sql.SQLException;
- import java.util.List;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- public class BaseHibernateDao<T,ID extends Serializable> extends HibernateDaoSupport implements BaseDao<T,ID> {
- @Override
- public void delete(T entity) {
- this.getHibernateTemplate().delete(entity);
- }
- @Override
- public void deleteById(Class<T> entityClass, ID id) {
- delete(this.findById(entityClass, id));
- }
- @Override
- public T findById(Class<T> entityClass, ID id) {
- return (T)this.getHibernateTemplate().get(entityClass, id);
- }
- @Override
- public List<T> findAll(Class<T> entityClass) {
- String name=entityClass.getName();
- return this.getHibernateTemplate().find("from"+name);
- }
- @Override
- public void save(T entity) {
- this.getHibernateTemplate().save(entity);
- }
- @Override
- public void update(T entity) {
- this.getHibernateTemplate().update(entity);
- }
- @Override
- public List<Object> find(String hql, Object... values) {
- return this.getHibernateTemplate().find(hql,values);
- }
- @Override
- public Page<T> findByPage(final String hql,final String countHql,final int currentPage) {
- // TODO Auto-generated method stub
- return (Page<T>)this.getHibernateTemplate().execute(new HibernateCallback(){
- @Override
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- //初始化Page
- Page<T> page=new Page<T>(getCount(session, countHql));
- page.setCurrentPage(currentPage);
- //分页查询开始
- Query query=session.createQuery(hql);
- query.setFirstResult(page.getStartRow());
- query.setMaxResults(page.getPageSize());
- //把取得的实体list设置到Page 类中
- page.setResult(query.list());
- return page;
- }
- //获取总记录数
- private int getCount(Session session,String countHql){
- Long count=0L;
- List list=session.createQuery(countHql).list();
- if(list!=null&&list.size()==1)
- count=(Long)list.get(0);
- return count.intValue();
- }
- });
- }
- }

浙公网安备 33010602011771号