michaelwang1978

BlogJava 首页 新随笔 联系 聚合 管理
  10 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks
Hibernate 提供了比较好的分页查询api极大地方便了使用者。以下是我在项目中写的一个Hibernate 分页查询的代码:
//根据Message 对象所封装的查询条件分页查询Message对象,S
public List getPageinMessage(Message message,int startRow,int rows) throws DAOException {       
        StringBuffer queryString=new StringBuffer(" from Message as message where ");
        //ActionExceptionHandler actionHandler=new ActionExceptionHandler();
        if(message.getSource()!=null){
            queryString.append("message.source.id="+message.getSource().getId());
            queryString.append(" and senderDelFlag=0");
        }else if(message.getTarget()!=null){
            //if message's target is not null query message base on target's id
            logger.debug("query inbox==================");
            queryString.append("message.target.id="+message.getTarget().getId());
            queryString.append(" and receiverDelFlag=0");
        } else{
            throw new BusinessException("未选定消息发送者或接受者"); 
        }
        queryString.append(" order by message.creatDate DESC");
        Query query=null;
        List messageList=null;
        Session session=this.getSession();       
        try {
            query=session.createQuery(queryString.toString());
            query.setFirstResult(startRow);
            query.setMaxResults(rows);
            logger.debug("first row===="+startRow+"===row amount==="+rows);
            messageList=query.list();
            if(messageList.size()>0){
                Message message2=(Message)messageList.get(0);              
                logger.debug("messageList's length==="+message2);           
                logger.debug("messageList's length==="+messageList.size());
            }
        } catch (HibernateException e) {
            // TODO Auto-generated catch block          
            e.printStackTrace();
            throw new BusinessException("读取页面数据发生错误"); 
        }finally{
            if(session!=null&&session.isOpen()){
                try {
                    session.close();
                } catch (HibernateException e1) {
                    e1.printStackTrace();
                    throw new BusinessException("会话关闭异常");                   
                }
            }
        }          
        return messageList;       
    }
posted on 2005-10-06 11:05 Michael 阅读(236) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: