Oracle数据库分页查询:
利用rownum和between and关键字
-- 查询员工表和薪水表的分页sql(pageNo:页号从1开始,pageSize:每页大小)
select*
from(selectROWNUM rNo,user_id,user_name,user_dept,user_salary from(selectinfo.user_name,salary.* fromzdy_userinfo info,zdy_salary salary whereinfo.user_id = salary.user_id order byinfo.user_id ) where ROWNUM <= pageSize * pageNo order by ROWNUM asc
)
where rNo between pageSize * (pageNo - 1) + 1 and pageSize * pageNo;
MySQL数据库分页查询:
利用两个limit关键字和子查询的方式(单表,海量数据时性能显著提升)
-- 查询第2页的数据(假设每页10条)
select*
fromt_course as t_cou
where t_cou.id >= (-- 第11条数据的idselect idfromt_course as t_cou2order by t_cou2.id limit 10, 1-- order by t_cou2.id limit pageSize * (pageNo - 1), 1)
order by id limit 10;
-- order by id limit pageSize;
MySQL实现Oracle中的rownum关键字的功能
SELECT@rownum := @rownum + 1 AS rownum,t_course.*
FROM(SELECT @rownum := 0) r, -- 多表的别名r不能省略(多表结构完整)t_course;
MySQL中分页总数据条数Total的处理:
利用sql_calc_found_rows关键字配合found_rows()函数(性能较高,需要在连接参数中指定allowMultiQueries=true即支持一次执行多条SQL语句)
SELECT SQL_CALC_FOUND_ROWS
t_cou.id,
t_cou.title,
t_cou.STATUS,
t_cou.price,
t_tea.NAME AS teacherName,
t_tea.intro
FROMt_course t_couLEFT OUTER JOIN t_teacher t_tea ON t_cou.teacher_id = t_tea.id
WHERE1 = 1 AND t_cou.is_deleted <> 1 AND t_cou.price >= 0 AND t_cou.price <= 99
ORDER BYt_cou.gmt_modified DESC LIMIT 0,3;
SELECTfound_rows() AS total;
Mybatis或Mybatis-Plus中使用示例:
<resultMap id="courseSearchResultMap" type="com.wondersgroup.edu.entity.vo.CourseSearchResultVo"><id property="id" column="id"></id><result property="title" column="title"></result><result property="status" column="status"></result><result property="teacherName" column="teacherName"></result><result property="price" column="price"></result><result property="intro" column="intro"></result></resultMap><resultMap id="countResultMap" type="Long"><result property="count" column="total"></result></resultMap><!--条件分页查询课程列表信息--><!--由于是多参数传入,所以不需要对parameterType进行配置--><select id="getListCourse" resultMap="courseSearchResultMap, countResultMap"><!--<bind name="offset" value="(current - 1) * size"></bind>-->selectsql_calc_found_rowst_cou.id,t_cou.title,t_cou.status,t_cou.price,t_tea.name as teacherName,t_tea.introfromt_course t_couleft outer join t_teacher t_tea on t_cou.teacher_id = t_tea.idwhere 1 = 1 and t_cou.is_deleted <> 1<if test="courseSearchVo.title != null and courseSearchVo.title != ''">and t_cou.title like concat('%', #{courseSearchVo.title}, '%')</if><if test="courseSearchVo.status != null and courseSearchVo.status != ''">and t_cou.status = #{courseSearchVo.status}</if><if test="courseSearchVo.teacherName != null and courseSearchVo.teacherName != ''">and t_tea.name like concat('%', #{courseSearchVo.teacherName}, '%')</if><if test="courseSearchVo.priceLow != null and courseSearchVo.priceLow != ''">and t_cou.price >= #{courseSearchVo.priceLow}</if><if test="courseSearchVo.priceHigh != null and courseSearchVo.priceHigh != ''">and t_cou.price <= #{courseSearchVo.priceHigh}</if>order by t_cou.gmt_modified desc limit ${(current - 1) * size}, #{size};select found_rows() as total;</select>
服务端获取分页数据集和分页信息:
List<List<?>> pageList = (List<List<?>>)courseService.pageQuery(courseSearchVo, current, size);// 分页total采用mysql提供的sql_calc_found_rows关键字配合found_rows()函数List<CourseSearchResultVo> dataList = (List<CourseSearchResultVo>) pageList.get(0);Long total = (Long) pageList.get(1).get(0);