SQLServer2005+Struts+Hibernate+Spring jsp页面中如何进行分页显示

如题,使用以上数据库和框架,如何进行分页显示。新手,对分页完全不懂,最好是有完整点的例子。。... 如题,使用以上数据库和框架,如何进行分页显示。
新手,对分页完全不懂,最好是有完整点的例子。。
展开
 我来答
惊奇汇
2009-05-19
知道答主
回答量:10
采纳率:0%
帮助的人:8.3万
展开全部
package com.highesthelp.framework.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;

import com.highesthelp.framework.common.Page;
import com.highesthelp.framework.exception.DataAccessException;

/**
* @copyright:最高助力 版权所有 未经许可,不得以任何方式复制或使用本程序任何部分
* @author: jackie
* @email: <a href="mailto:jackstudiomaster@gmail.com">最高助力</a>
* @createTime: Apr 15, 2008
* @description:
*/
public class BaseDao extends HibernateDaoSupport {

public Log log = LogFactory.getLog(this.getClass());

/***************************************************************************
* 通过主健(Short)获取单条数据
*
* @param cls
* @param pk
* @return
* @throws DataAccessException
*/
public Object getObjectByPk(Class cls, Short pk) throws DataAccessException {
try {
log
.info("get " + cls.getName() + " from database,it's pk is "
+ pk);
Object obj = this.getHibernateTemplate().get(cls, pk);
return obj;
} catch (Exception e) {
log
.error("Failture: error getObjectByPk from database ,it's pk is :"
+ pk + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过主健(String)获取单条数据
*
* @param cls
* @param pk
* @return
* @throws DataAccessException
*/
public Object getObjectByPk(Class cls, String pk)
throws DataAccessException {
try {
log
.info("get " + cls.getName() + " from database,it's pk is "
+ pk);
Object obj = this.getHibernateTemplate().get(cls, pk);
return obj;
} catch (Exception e) {
log
.error("Failture: error getObjectByPk from database ,it's pk is :"
+ pk + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过主健(Long)获取单条数据
*
* @param cls
* @param pk
* @return
* @throws DataAccessException
*/
public Object getObjectByPk(Class cls, Long pk) throws DataAccessException {
try {
log
.info("get " + cls.getName() + " from database,it's pk is "
+ pk);
Object obj = this.getHibernateTemplate().get(cls, pk);
return obj;
} catch (Exception e) {
log
.error("Failture: error getObjectByPk from database ,it's pk is :"
+ pk + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过主健(Float)获取单条数据
*
* @param cls
* @param pk
* @return
* @throws DataAccessException
*/
public Object getObjectByPk(Class cls, Float pk) throws DataAccessException {
try {
log
.info("get " + cls.getName() + " from database,it's pk is "
+ pk);
Object obj = this.getHibernateTemplate().get(cls, pk);
return obj;
} catch (Exception e) {
log
.error("Failture: error getObjectByPk from database ,it's pk is :"
+ pk + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过主健(Double)获取单条数据
*
* @param cls
* @param pk
* @return
* @throws DataAccessException
*/
public Object getObjectByPk(Class cls, Double pk)
throws DataAccessException {
try {
log
.info("get " + cls.getName() + " from database,it's pk is "
+ pk);
Object obj = this.getHibernateTemplate().get(cls, pk);
return obj;
} catch (Exception e) {
log
.error("Failture: error getObjectByPk from database ,it's pk is :"
+ pk + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过主健(Integer)获取单条数据
*
* @param cls
* @param pk
* @return
* @throws DataAccessException
*/
public Object getObjectByPk(Class cls, Integer pk)
throws DataAccessException {
try {
log
.info("get " + cls.getName() + " from database,it's pk is "
+ pk);
Object obj = this.getHibernateTemplate().get(cls, pk);
return obj;
} catch (Exception e) {
log
.error("Failture: error getObjectByPk from database ,it's pk is :"
+ pk + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过主健获取单条数据
*
* @param obj
* @param pk
* @return
* @throws DataAccessException
*/
public Object getObjectByPk(Object obj, Serializable pk)
throws DataAccessException {
try {
log.info("get " + obj.getClass().getName()
+ " from database,it's pk is " + pk.toString());
Object o = this.getHibernateTemplate().get(obj.getClass(), pk);
return o;
} catch (Exception e) {
log
.error("Failture: error getObjectByPk from database ,it's pk is :"
+ pk + " exception info: " + e);
throw new DataAccessException();
}

}

/***************************************************************************
* 通过SQL获取数据列表
*
* @param hql
* @return ArrayList
* @throws DataAccessException
*/
public List getObjectsList(String hql) throws DataAccessException {
try {
log.info("get list from database,it's hql is :" + hql);
List list = new ArrayList();
list = this.getHibernateTemplate().find(hql);
return list;
} catch (Exception e) {
log
.error("Failture: error getObjectsList from database ,it's hql is :"
+ hql + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过SQL获取单个数据
*
* @param hql
* @return ArrayList
* @throws DataAccessException
*/
public Object getObject(String hql) throws DataAccessException {
try {
log.info("get Object from database,it's hql is :" + hql);
List list = new ArrayList();
list = this.getHibernateTemplate().find(hql);
if(list != null && list.size() > 0){
return list.get(0);
}
return null;
} catch (Exception e) {
log
.error("Failture: error getObjectsList from database ,it's hql is :"
+ hql + " exception info: " + e);
throw new DataAccessException();
}
}

/***************************************************************************
* 通过SQL获取数据列表,可带数组参数
*
* @param hql
* @param obj
* @return
* @throws DataAccessException
*/
public List getObjectsList(String hql, Object[] obj)
throws DataAccessException {
try {
log.info("get list from database,it's hql is :" + hql);
List list = new ArrayList();
list = this.getHibernateTemplate().find(hql, obj);
return list;
} catch (Exception e) {
log
.error("Failture: error getObjectsList from database ,it's hql is :"
+ hql
+ " and parameter is"
+ obj.toString()
+ " exception info: " + e);
throw new DataAccessException();
}

}

/***************************************************************************
* 翻页获取数据列表
*
* @param hql
* @param startIndex
* @param pageSize
* @return
* @throws DataAccessException
*/
public List getObjectsListPage(final String hql, final int startIndex,
final int pageSize) throws DataAccessException {
try {
List list = this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
try {
Query query = session.createQuery(hql);
query.setFirstResult(startIndex);
query.setMaxResults(pageSize);
List innerlist = query.list();
log.info("get list from database it's hql is :"
+ hql + ", startIndex = " + startIndex
+ " pageSize = " + pageSize);
return innerlist;
} catch (Exception e) {
log.error(
"failture: get list from database,it's hql is :"
+ hql + ", startIndex = "
+ startIndex + " pageSize = "
+ pageSize, e);
throw new SQLException();
}
}
});
return list;
} catch (Exception e) {
log
.error("Failture: error getObjectsListPage from database ,it's hql is :"
+ hql + " exception info: " + e);
throw new DataAccessException();
}

}

/***************************************************************************
* 获取翻页数据列表
*
* @param hql
* @param startIndex
* @param pageSize
* @return Page
* @throws DataAccessException
*/
@SuppressWarnings("unchecked")
public Page getBaseDaoPage(String hql, int startIndex, int pageSize)
throws DataAccessException {
try {
StringBuffer bufferString = new StringBuffer("select count(*) ");
String tempStr = hql.toLowerCase();
int fromIndex = tempStr.indexOf("from ");
int orderIndex = tempStr.indexOf(" order ");
int limitIndex = tempStr.indexOf(" limit ");
if (orderIndex > 0) {
bufferString.append(hql.substring(fromIndex, orderIndex));
} else {
if (limitIndex <= 0) {
bufferString.append(hql.substring(fromIndex));
} else {
bufferString.append(hql.substring(fromIndex, limitIndex));
}
}
Page page = new Page(startIndex, pageSize);
List list = getObjectsListPage(hql, startIndex, pageSize);
page.setResult(list);
int totalCount = getHQLCount(bufferString.toString());
page.setTotalCount(totalCount);
return page;
} catch (Exception e) {
log
.error("Failture: error getBaseDaoPage from database ,it's hql is :"
+ hql + " exception info: " + e);
throw new DataAccessException(e.getMessage());
}

}

/***************************************************************************
* 保存对象
*
* @param obj
* @return Object
* @throws DataAccessException
*/
public Object saveObject(Object obj) throws DataAccessException {
try {
log.info("save object to database,it's name is :"
+ obj.getClass().getName());

this.getHibernateTemplate().save(obj);
return null;
} catch (Exception e) {
log.error("Failture: error saveObject to database ,it is :"
+ obj.toString() + " exception info: " + e);
throw new DataAccessException(e.getMessage());
}

}

/***************************************************************************
* 保存对象集合
*
* @param list
* @throws DataAccessException
*/
public void saveObjectsList(List list) throws DataAccessException {
try {
log.info("save object (List) to database");
this.getHibernateTemplate().saveOrUpdateAll(list);
} catch (Exception e) {
log.error("Failture: error saveObject to database ,it is :"
+ list.toString() + " exception info: " + e);
throw new DataAccessException(e.getMessage());
}
}

/***************************************************************************
* 删除对象
*
* @param obj
* @throws DataAccessException
*/
public void deleteObject(Object obj) throws DataAccessException {
try {
log.info("delete object from database,it's name is :"
+ obj.getClass().getName());
this.getHibernateTemplate().delete(obj);
} catch (Exception e) {
log
.error("Failture: error deleteObject from database,it's name is :"
+ obj.getClass().getName()
+ " exception info: "
+ e);
throw new DataAccessException(e.getMessage());
}

}

/***************************************************************************
* 执行HQL语句
*
* @param hql
* @return
* @throws DataAccessException
*/
public int excuteHQL(String hql) throws DataAccessException {
try {
Session session = this.getHibernateTemplate().getSessionFactory()
.openSession();
int effect = 0;
// 事物全部交有Spring配置来管理
/*
* session.setFlushMode(FlushMode.COMMIT); Transaction tx =
* session.beginTransaction();
*/
try {
Query query = session.createQuery(hql);
effect = query.executeUpdate();
log.info("execute effect result = " + effect + " hql =" + hql);

/* tx.commit(); */
} catch (Exception e) {
/* tx.rollback(); */
log
.error("Failture: error delete/update to database ,it's hql is :"
+ hql);
throw new DataAccessException(e.getMessage());
} finally {
session.close();
}
return effect;
} catch (Exception e) {
log
.error("Failture: error deleteObject from database,it's hql is :"
+ hql + " exception info: " + e);
throw new DataAccessException(e.getMessage());
}
}

/***************************************************************************
* 更新对象
*
* @param obj
* @throws DataAccessException
*/
public Object updateObject(Object obj) throws DataAccessException {
try {
log.info("update Object to database , it's name is :"
+ obj.getClass().getName());
this.getHibernateTemplate().update(obj);
return obj;
} catch (Exception e) {
log.error("Failture: error updateObject to database,it's obj is :"
+ obj.toString() + " exception info: " + e);
throw new DataAccessException(e.getMessage());
}

}

/***************************************************************************
* 更新对象列表
*
* @param list
* @throws DataAccessException
*/
public void updateObjectsList(List list) throws DataAccessException {
try {
log.info("update Object (list) to database");
this.getHibernateTemplate().saveOrUpdateAll(list);
} catch (Exception e) {
log
.error("Failture: error updateObjectsList to database,it's obj is :"
+ list.toString() + " exception info: " + e);
throw new DataAccessException(e.getMessage());
}

}

/***************************************************************************
* 查询 count(*)
*
* @param hql
* @return
* @throws DataAccessException
*/
public int getHQLCount(String hql) throws DataAccessException {
int count = 0;
try {
List list = this.getHibernateTemplate().find(hql);
Object obj = (Object) list.get(0);
count = Integer.valueOf(obj.toString()).intValue();
} catch (Exception e) {
log
.error("Failture: error get HQL Count from database ,it's hql is :"
+ hql + " exception info: " + e);
throw new DataAccessException(e.getMessage());
}
return count;
}

public Session getHibernateSession(){
return this.getSession();
}

/***************************************************************************
* 获取数据库连接
*/
public Connection getConnection() {
Connection conn = this.getSession().connection();
return conn;
}

}

这是我自己平时用的,楼主可以看看。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式