我用如下方法,进行分页查询.
可是兴果老是不对.
如:我传入第一页的数据:start=0,end=2,结果list.size=2,到页面也是2条(正确的)第2页数据: 传入start=2,end=4,结果list.size=4,到页面也是4条(错误!重复第1页面2条)
第3页数据:  传入start=4,end=6,结果list.size=6,到页面也是6条(重复第2页2条,并且还多查了2条)
第4页数据:  传入start=6,end=8,结果list.size=4,到页面也是4条(重复第3页2条)
第5页数据:  传入start=8,end=10,结果list.size=2,到页面也是2条(是重复前面的数据)控制台打印出来的SQL语句如下:
select * from ( select row_.*, rownum rownum_ from ( select topic0_.ID as ID, topic0_.USERID as USERID3_, topic0_.BANKUAIID as BANKUAIID3_, topic0_.SUBJECT as SUBJECT3_, topic0_.CONTENT as CONTENT3_, topic0_.REPLYNUM as REPLYNUM3_ from KILLER.TOPIC topic0_ order by topic0_.ID desc ) row_ where rownum <= ?) where rownum_ > ?用debug跟踪数据发现start和end在执行前后,并没有改变.急用,在线等.
QQ号:181057908,谢谢.public List queryTopic(final int start,final int end,final String flag) {
HibernateTemplate ht = this.getHibernateTemplate();
return (List) ht.execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String hql="";
if(flag.equals("all")){
hql="from Topic order by id desc";
}else if(flag.equals("order")){
hql="from Topic order by replynum desc";
}
Query query=session.createQuery(hql);
query.setFirstResult(start);
query.setMaxResults(end);
List list = new ArrayList();
list.clear();
list = query.list();
return list;
}
        });
}

解决方案 »

  1.   

    对于你的问题,我看的不太明白。
    1.rownum是源于哪张表的,是KILLER.TOPIC 吗?
    2.筛选条件语义不符,rownum如果是一列属性的话,应该是固定。不论正序还是反序都一样。难道rownum是动态生成的?但是,对于分页,我提供如下方法:
    with temp_table as  
    ( target_table ) 
    select top  span_number * 
    from  temp_table 
    where temp_table.id not in ( select top start_row_number temp_table.id from temp_table order by temp_table.id )  target_table 是想要被分页的表,当然,你要重命名主键,(这里统一成id了,你也可以自己定,但是所有的temp_table.id都要变)
    start_row_number 就是起始行号,类型为整形。
    start_span 就是分页大小,页面要显示多少行,类型为整形。
      

  2.   


    我用的是oracle数据库,所以会有这个字段.
      

  3.   

    首先说明一下,lz对start和end的理解有误,才认为分页结果错误。
    start——指的是数据索引的开始位置(从0开始),这点lz没有问题;
    end——其实不应该叫end,而是maxResults,指的是返回的数据条数,而不是结束位置;查询分析如下:
    1 2 3 4 5 6 7 8 9 10                ——数据编号
    0 1 2 3 4 5 6 7 8 9                 ——数据索引
    * *                                 第1页;start=0;end=2;
        * * * *                         第2页;start=2;end=4;
            * * * * * *                 第3页;start=4;end=6;
                * * * * ? ? ? ?         第4页;start=6;end=8;
                    * * ? ? ...         第5页;start=8;end=10;从lz的分析可知数据库中应该只有10条记录。
    从上面的分析可以看出分页结果没有问题,是lz理解出错啦、、、
    这里看起来好像有点问题,lz再检查下,看是否是写错了还是数据本身就是一样的。
      

  4.   


    查询分析如下:
    1 2 3 4 5 6 7 8 9 10              ——数据编号
    0 1 2 3 4 5 6 7 8 9               ——数据索引
    * *                               第1页;start=0;end=2;
        * * * *                       第2页;start=2;end=4;
            * * * * * *               第3页;start=4;end=6;
                * * * * ? ? ? ?       第4页;start=6;end=8;
                    * * ? ? ...       第5页;start=8;end=10;排版有问题,哈哈!
      

  5.   


    谢谢,就是这个问题.
    maxResults,这个方法我没有掌握.
    昨晚最后在1:40左右解决的.
    还是谢谢哈.也谢谢大家.