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

相关推荐
远歌已逝2 小时前
维护在线重做日志(二)
数据库·oracle
qq_433099403 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz3 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
工业甲酰苯胺5 小时前
Redis性能优化的18招
数据库·redis·性能优化
冰帝海岸5 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象5 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端