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;这样也方便每次获取总页数和总条数。

相关推荐
KATA~3 分钟前
解决MyBatis-Plus枚举映射错误:No enum constant问题
java·数据库·mybatis
xyliiiiiL19 分钟前
一文总结常见项目排查
java·服务器·数据库
shaoing20 分钟前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
用户62799471826221 分钟前
南大通用GBase 8s 获取表的约束与索引列信息
数据库
Arbori_2621540 分钟前
获取oracle表大小
数据库·oracle
王强你强1 小时前
MySQL 高级查询:JOIN、子查询、窗口函数
数据库·mysql
草巾冒小子1 小时前
brew 安装mysql,启动,停止,重启
数据库·mysql
用户6279947182621 小时前
南大通用GBase 8c分布式版本gha_ctl 命令-HI参数详解
数据库
斯汤雷1 小时前
Matlab绘图案例,设置图片大小,坐标轴比例为黄金比
数据库·人工智能·算法·matlab·信息可视化
腥臭腐朽的日子熠熠生辉1 小时前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven