暫且把廢話放一邊,代碼上:
/**
* 上一條記錄
* @param id
* @return
*/
@Override
public Object preBlog(String id) {
final String fid = id;
final String sql = "from Blog b where b.bid<? order by b.bid desc";
return this.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Object obj = session.createQuery(sql).setString(0, fid).setMaxResults(1).uniqueResult();
System.out.println(((Blog)obj).getBid());
return obj;
}
});
}
/**
* 下一條記錄
* @param id
* @return
*/
@Override
public Object nextBlog(String id) {
final String fid = id;
final String sql = "from Blog b where b.bid>? order by b.bid asc";
return this.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Object obj = session.createQuery(sql).setString(0, fid).setMaxResults(1).uniqueResult();
System.out.println(((Blog)obj).getBid());
return obj;
}
});
}
其實我這種思想是利用了mysql分頁的sql語法,小于當前id的數據的所有的i的降序排列,自然第一條就是當前數據鄰近的上一條,同理可得下一條!!