SQL Server分页查询

原生SQL语句,实现SQL Server分页查询

sql 复制代码
DECLARE @pageCount INT 
DECLARE @pageIndex INT 
SET @pageCount = 10 -- #{page.pageSize}
SET @pageIndex = 2 -- #{page.pageNum}
;WITH ACTE AS (
	SELECT
		ROW_NUMBER () OVER (ORDER BY id) AS SN,
		id, table_id, name, workcode, create_time, update_time
	FROM
		lott_result_information
	),
BCTE AS (SELECT CEILING(MAX(SN) * 1.0 / @pageCount) AS PageTotal FROM ACTE) 
SELECT *,
	(SELECT COUNT (*) FROM ACTE ) AS total_number,
	(SELECT PageTotal FROM BCTE ) AS total_page_size 
FROM
	ACTE 
WHERE
	ACTE.SN > (@pageIndex - 1) * @pageCount 
	AND ACTE.SN <= (@pageIndex * @pageCount)

实战

  1. 编写Mapper.java的接口
java 复制代码
List<LottResultInformation> selectLottResultByPage(@Param("name") String name, @Param("page") BasePage page);
  1. 编写Mapper.xml的SQL语句
XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.framework.mapper.LottMapper">
    <resultMap id="BaseResultLottResultMap" type="com.demo.framework.entity.LottResultInformation"  extends="BasePageMap">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="table_id" property="tableId" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="workcode" property="workcode" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="BasePageMap" type="com.demo.framework.entity.BasePage">
        <result column="total_number" property="totalNumber" jdbcType="INTEGER"/>
        <result column="total_page_size" property="pageTotal" jdbcType="INTEGER"/>
    </resultMap>

    <select id="selectLottResultByPage" resultMap="BaseResultLottResultMap">
        DECLARE @pageCount INT
        DECLARE @pageIndex INT
        SET @pageCount = #{page.pageSize}
        SET @pageIndex = #{page.pageNum}
        ;WITH ACTE AS (
            SELECT ROW_NUMBER() OVER (ORDER BY id) AS SN,
                   id, table_id, name, workcode, create_time, update_time
            FROM lott_result_information
            <if test="name != null and name !=''">
                where name LIKE CONCAT('%',#{name},'%')
            </if>
        ),
        BCTE AS (SELECT CEILING(MAX(SN) * 1.0 / @pageCount) AS PageTotal FROM ACTE)
        SELECT *,
               (SELECT COUNT(*) FROM ACTE)  AS total_number,
               (SELECT PageTotal FROM BCTE) AS total_page_size
        FROM ACTE
        WHERE ACTE.SN > (@pageIndex - 1) * @pageCount
          AND ACTE.SN &lt;= (@pageIndex * @pageCount)
    </select>

</mapper>

这样的话每次查询都会冗余两列total_number和total_page_size;这样也方便每次获取总页数和总条数。

相关推荐
lzhlizihang3 分钟前
【Hive sql 面试题】求出各类型专利top 10申请人,以及对应的专利申请数(难)
大数据·hive·sql·面试题
无尽的大道4 分钟前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
小鑫记得努力13 分钟前
Java类和对象(下篇)
java
binishuaio17 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE19 分钟前
【Java SE】StringBuffer
java·开发语言
老友@19 分钟前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose
superman超哥27 分钟前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
wrx繁星点点34 分钟前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
Upaaui37 分钟前
Aop+自定义注解实现数据字典映射
java
zzzgd81637 分钟前
easyexcel实现自定义的策略类, 最后追加错误提示列, 自适应列宽,自动合并重复单元格, 美化表头
java·excel·表格·easyexcel·导入导出