您的位置:首页 > 移动开发 > Objective-C

使用 HibernateTemplate 实现分页查询 (HibernateCallback接口)

2011-11-23 09:49 543 查看

/**


 * 


 */


package springdao;




import hibernatedao.HibernateSessionFactory;


import java.sql.SQLException;


import java.util.List;


import org.apache.commons.logging.Log;


import org.apache.commons.logging.LogFactory;


import org.hibernate.HibernateException;


import org.hibernate.Session;


import org.springframework.orm.hibernate3.HibernateCallback;


import org.springframework.orm.hibernate3.HibernateTemplate;








/**


 * 通用 DAO 包


 * @author kiant


 * @version Sep 7, 2008


 */


public class CommomsDAO {


    private static final Log log = LogFactory.getLog(EcOpusDAO.class);


    //获得会话


    private static HibernateTemplate hibernateTemplate = new HibernateTemplate(HibernateSessionFactory.getSessionFactory());


    


    


    /**


     * 分页通用方法


     * @param hql  HQL查询语句


     * @param offset    起始记录下标


     * @param lengh        读取记录数


     * @return            List 结果集


     */


    public static List getListForPage(final String hql, final int offset, final int lengh) {


        log.debug("finding ListForPage");


        try {


            List list = hibernateTemplate.executeFind(new HibernateCallback(){




                public Object doInHibernate(Session session)


                        throws HibernateException, SQLException {


                    List list2 = session.createQuery(hql)


                            .setFirstResult(offset)


                            .setMaxResults(lengh)


                            .list();                    


                    return list2;


                }});


            return list;


        } catch (RuntimeException re) {


            log.error("find ListForPage failed", re);


            throw re;


        }


    }


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  session dao object 数据库