Hibernate获取'上一条'和'下一条'记录

来源:互联网 发布:淘宝网html模板 编辑:程序博客网 时间:2024/04/28 15:27
 

Hibernate获取'上一条'和'下一条'记录

      暂且把废话放一边,代码上:

view sourceprint?
/**
     * 上一条记录
     * @param id
     * @return 
     */
    @Override
    publicObject preBlog(String id) {
        finalString fid = id;
        finalString sql = "from Blog b where b.bid<? order by b.bid desc";
        returnthis.getHibernateTemplate().execute(newHibernateCallback() {
            @Override
            publicObject doInHibernate(Session session)
                    throwsHibernateException, SQLException {
                Object obj = session.createQuery(sql).setString(0, fid).setMaxResults(1).uniqueResult();
                System.out.println(((Blog)obj).getBid());
                returnobj;
            }
        });
    }
    /**
     * 下一条记录
     * @param id
     * @return 
     */
    @Override
    publicObject nextBlog(String id) {
        finalString fid = id;
        finalString sql = "from Blog b where b.bid>? order by b.bid asc";
        returnthis.getHibernateTemplate().execute(newHibernateCallback() {
            @Override
            publicObject doInHibernate(Session session)
                    throwsHibernateException, SQLException {
                Object obj = session.createQuery(sql).setString(0, fid).setMaxResults(1).uniqueResult();
                System.out.println(((Blog)obj).getBid());
                returnobj;
            }
        });
    }

     其实我这种思想是利用了mysql分页的sql语法,小于当前id的数据的所有的i的降序排列,自然第一条就是当前数据邻近的上一条,同理可得下一条!!

原创粉丝点击