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

相关推荐
转码的小石3 分钟前
12/21java基础
java
李小白6611 分钟前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp24 分钟前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
独行soc29 分钟前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍08-基于时间延迟的SQL注入(Time-Based SQL Injection)
数据库·sql·安全·渗透测试·漏洞挖掘
White_Mountain1 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
Code apprenticeship1 小时前
怎么利用Redis实现延时队列?
数据库·redis·缓存
百度智能云技术站1 小时前
广告投放系统成本降低 70%+,基于 Redis 容量型数据库 PegaDB 的方案设计和业务实践
数据库·redis·oracle
装不满的克莱因瓶1 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
n北斗1 小时前
常用类晨考day15
java
骇客野人1 小时前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言