`
micheal19840929
  • 浏览: 166468 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Hibernate中显示前一条,后一条记录

    博客分类:
  • J2EE
阅读更多

哪位有更好的方法,贡献下,谢了先!

  
/**
   * 查询当前记录的前一条记录,并返回主键id
   * 
   * @author Neeao(njl) 2007-7-17
   * @param id
   * @return
   * @throws DAOException
   */
  public int before(int id) throws DAOException {
    int strid = 0;
    Session session = SessionManager.getSession(); 

    String hql = "from MailList as maillist where maillist.id<" + id
        + " order by maillist.id desc";
    Query query = session.createQuery(hql);
    query.setFirstResult(0);
    query.setMaxResults(1);
    List list = query.list();

    if (list != null && list.size() > 0) {
      MailList mailList = (MailList) list.get(0);
      strid = mailList.getId();
    }
    return strid;

  }

  /**
   * 查询当前记录的下一条记录,并返回主键id
   * 
   * @author Neeao(njl) 2007-7-17
   * @param id
   * @return
   * @throws DAOException
   */
  public int next(int id) throws DAOException {
    int strid = 0;
    Session session = SessionManager.getSession();
    String hql = "from MailList as maillist where maillist.id> " + id
        + " order by maillist.id";
    Query query = session.createQuery(hql);
    query.setFirstResult(0);
    query.setMaxResults(1);
    List list = query.list();

    if (list != null && list.size() > 0) {
      MailList mailList = (MailList) list.get(0);
      strid = mailList.getId();
    }
    return strid;

  }
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics