SQL分页和HQL分页查询

SQL分页

 1 -- 每页展示5条数据
 2 -- 数据  总条数:totalCount 每页显示条数:pageSize     currPage:当前页数    起始索引:startIndex  总页数:totalPage
 3 -- 总页数计算公式:
 4                                             -- 方法一
 5                                                 --  totalCount%pageSize  如果余数为0,totalPage=totalCount/pageSize  
 6                                                 -- 如果余数不为0 ,totalPage=totalCount/pageSize +1  
 7                                           -- 方法二
 8                                                 -- totalPage= (totalCount + pageSize - 1)/pageSize
 9 -- 查询每页数据
10             -- (currPage - 1) * pageSize
11 
12 SELECT COUNT(*) FROM stuscore 
13 
14 SELECT * FROM stuscore LIMIT 0,5 ;-- 第一页数据
15 SELECT * FROM stuscore LIMIT 5,5 ;-- 第二页数据
16 SELECT * FROM stuscore LIMIT 10,5 ;-- 第三页数据
17 
18 SELECT * FROM stuscore where 1=1 AND  grade LIKE '%22%' AND sex='' AND score >=20 AND score < 70 LIMIT 0,5 
19 
20 SELECT * FROM stuscore where 1=1  and  grade LIKE '%22%'  AND sex=''  AND score >=20 AND score <70 LIMIT 0,5 

HQL分页

    int currentPage = 1; //当前页
        int pageCount = 4;  //每页的行数
        int startNum = (currentPage - 1) * pageCount; //起始的位置
        List<PlayerEntity> list =
                session.createQuery(from PlayerEntity)
                        .setFirstResult(startNum) //起始位置
                        .setMaxResults(pageCount) //每页显示的行数
                   .list();
setFirstResult(int firstResult)方法
      设置第一条记录的位置
setMaxResults(int maxResults)方法
    设置最大返回的记录条数
    

猜你喜欢

转载自www.cnblogs.com/xieshilin/p/12028438.html