您的当前位置:首页正文

Hibernate分页查询

2023-06-23 来源:小奈知识网


问题 : 现在要用过User表,把所有表中的对象加载到JavaBean中。 而且要通过分页来实现。下面程序我想用HQL方法实现这个分页,但 是我无法将roles表和funs表连接起来,role_fun是一个中间表,没有写 hibernate配置文件。根据以下程序生成的sql是这样的,外层的select *语 句限制内层要把所有表查出来,因为内层字段有外键约束。 select

* from

( select

users0_.user_id as user1_0_, users0_.username as username0_, users0_.user_status as user3_0_, users0_.role_id as role4_0_, users0_.password as password0_, users0_.user_describe as user6_0_, users0_.init_time as init7_0_, users0_.check_time as check8_0_, users0_.check_manager as check9_0_, users0_.ROLE_ID as ROLE4_0_ from

BANK_USERS users0_ ) where

rownum <= ?

1

public abstract class GenericHibernateDao extends HibernateDaoSupport implements GenericDao { /** * @return List 返回分页查询后的对象 * @param Page.PAGE_SIZE 每所包含的记录数 * @param pageNumber 页号 */ @Override public List pagination() { Query q = getSession().createQuery(\"FROM Users u order by u.user_id asc\"); q.setMaxResults(Page.PAGE_SIZE); q.setFirstResult(Page.PAGE_SIZE * (pageNumber - 1)); return q.list(); } static class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] { \"applicationContext.xml\" }); @SuppressWarnings(\"unchecked\") GenericDao dao = (GenericDao) ctx.getBean(\"usersDao\"); List list = dao.pagination(); System.out.println(list.get(0).getUsername()); } } }

2

因篇幅问题不能全部显示,请点此查看更多更全内容