hibernate+struts2分页查询的问题,永远都是第一页,为什么第一条sql语句对,第二条变样了
首先sql语句第1条select top 3 people0_.ID_ as ID1_0_, people0_.uname as uname0_, people0_.password as password0_, people0_.repassword as repassword0_ from People people0_
第2条select top 6 people0_.ID_ as ID1_2_, people0_.uname as uname2_, people0_.password as password2_, people0_.repassword as repassword2_ from People people0_为什么第2条sql语句是查询6条,而不是3条呢
也就是说为什么我的pageSize增加了一倍,我是要排除(pageNow-1)*pageSize条,查询pageSize条,求解啊
private pageNow=1;
private pageSize=3;(set和get方法默认)
public   List find(int pageNow,int pageSize){
Configuration c=new Configuration();
c.configure();
SessionFactory f=c.buildSessionFactory();
Session session=f.getCurrentSession();
@SuppressWarnings("unused")
Transaction r=session.beginTransaction();
Query query= session.createQuery("from People");
query.setFirstResult(pageSize*(pageNow-1));
query.setMaxResults(pageSize);
List list=query.list();
session.close();
return list;

解决方案 »

  1.   

    他贴出来的那是hibernate自己封装的SQL语句lz就你贴出来的这部分代码看不出问题所在,代码没有什么问题
    一般最好把你自己定好的pageSize作为常量定义在接口中
      

  2.   

    分页不是这么一句写的吧,select top                 页大小 
            people0_.ID_        as ID1_0_,
           people0_.uname      as uname0_,
           people0_.password   as password0_,
           people0_.repassword as repassword0_
      from People people0_
     where people0_id not in (select top 页大小 * (页数 - 1) id
                                from People people0_
                               order by people0_id)
    这貌似才是分页语句的写法吧