码迷,mamicode.com
首页 > 数据库 > 详细

SQL分页和HQL分页查询

时间:2019-12-12 13:08:29      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:where   index   sql   player   第一条   create   count   nbsp   font   

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)方法
    设置最大返回的记录条数
    

 

 

SQL分页和HQL分页查询

标签:where   index   sql   player   第一条   create   count   nbsp   font   

原文地址:https://www.cnblogs.com/xieshilin/p/12028438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!