sqlserver实现分页的几种方式

sqlserver实现分页的几种方式

第一种:使用org.springframework.data.domain.Page来进行分页

package com.cellstrain.icell.repository.repositoryImpl;

import com.cellstrain.icell.entity.V_Paper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

@Repository(value = "vPaperRepository")
public class VPaperRepositoryImpl {
@Autowired
private JdbcTemplate jdbcTemplate;

@PersistenceContext
private EntityManager entityManager;

/**
* 后台分页查询
* @param condition
* @param pageable
* @return
*/
public Page findByCondition(String condition, Pageable pageable){
StringBuffer hql = new StringBuffer("from V_Paper p where ");
if(!StringUtils.isEmpty(condition)){
hql.append("p.title like '%"+condition+"%' or p.entryName like '%"+condition+"%' or p.pName like '%"+condition+"%' or p.auth like '%"+condition+"%' order by p.paperId desc");
}else{
hql.append("1=1 order by p.paperId desc");
}
Query query = entityManager.createQuery(hql.toString());
//得到符合记录的总数
int count = query.getResultList().size();
Long total = (long)count;
//分页查询
query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
List<V_Paper> proteinRanksList = query.getResultList();
//封装Page
Page<V_Paper> page =new PageImpl<V_Paper>(proteinRanksList,pageable,total);
}

}

第二种:使用top关键字来进行分页
package com.cellstrain.icell.repository.repositoryImpl;

import com.cellstrain.icell.entity.V_Paper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

@Repository(value = "vPaperRepository")
public class VPaperRepositoryImpl {
@Autowired
private JdbcTemplate jdbcTemplate;

@PersistenceContext
private EntityManager entityManager;

/**
* 后台分页查询
* @param condition
* @param pageable
* @return
*/
public Page findByCondition(String condition, Pageable pageable){
if(StringUtils.isEmpty(condition)){
condition="";
}
int pageSize=pageable.getPageSize();
int pageNow=pageable.getPageNumber();
String sql="select top "+pageSize+" vp.paperId,vp.title,vp.entryName,vp.source,vp.recordDate,vp.url,vp.auth,dbo.JoinStr(vp.paperId) as pNames" +
" from V_Paper vp where vp.paperId not in(select top "+pageSize*pageNow+" paperId from V_Paper order by paperId desc) and (title like '%"+condition+"%' or contents like '%"+condition+"%') " +
"group by vp.paperId,vp.title,vp.entryName,vp.source,vp.recordDate,vp.url,vp.auth order by vp.paperId desc";
List paperList=null;
Long total = jdbcTemplate.queryForObject("select count(*) from V_Paper",Long.class);
try{
paperList=jdbcTemplate.queryForList(sql);
}catch (Exception e){
e.printStackTrace();
}
Page page = new PageImpl(paperList, pageable, total);
return page;
}
}
 

转载于:https://www.cnblogs.com/qianzf/p/6781618.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416825.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

FastDFS部署及测试

目录 简介 部署 测试 简介 FastDFS是一款开源的分布式文件系统&#xff0c;功能主要包括&#xff1a;文件存储、文件同步、文件访问&#xff08;文件上传、文件下载&#xff09;等&#xff0c;解决了文件大容量存储和高性能访问的问题。FastDFS特别适合以文件为载体的在线服…

FastDFS java api调用

介绍 本文讲述如何通过java调用FastDFS的文件上传、下载及删除&#xff0c;提供示例代码。 编译fastdfs-client-java 需要环境git、jdk8、maven git clone https://github.com/happyfish100/fastdfs-client-java.git cd fastdfs-client-java/ mvn package 配置 target目录…

centos7下编译openjdk11

源码 https://github.com/unofficial-openjdk/openjdk/releases 直接下载特定的版本jdk-1128 编译环境centos7 unzip jdk11xxx.zip cd jdk11xxx #安装文档中描述的外部库要求&#xff08;External Library Requirements&#xff09; yum install -y freetype-devel cups-d…

基于wordpress搭建网站和基于nodejs自己搭建

帮朋友做一个下载站的网站&#xff0c;有两种方案&#xff1a; 1 基于wordpress 优势&#xff1a;自带后台&#xff0c;很多插件可用&#xff0c;同时网站结构、组织、分类系统、tag系统还是蛮实用的劣势&#xff1a;需要自定义post的字段&#xff0c;比如下载链接、名称&#…

fernflower idea内置的反编译工具

对比了多款反编译工具&#xff0c;还是觉得idea自带的fernflower好用&#xff0c;相比较之下java-decompiler无法反编译lambda表达式&#xff0c;而这个fernflower可以&#xff0c;所以在有lambda表达式的情况下&#xff0c;还是选择fernflower比较好。 这里上一个单独使用fer…

5.16魔幻方阵

Q&#xff1a;魔幻方阵指在n*n的矩阵中填写1~n*n这些数字&#xff0c;使得每行每列以及两个对角线之和均相等。编写一个程序&#xff0c;打印出一种三阶的魔幻方阵。 思路一&#xff1a; //用来判断i~q是否互不相等 int match(int i,int j,int k,int l,int m,int n,int o, int …

centos7编译 openjdk8

下载地址 http://jdk.java.net/java-se-ri/8 找到 The source code of the RI binaries is available under the GPLv2 in a single zip file (md5) 121 MB. 点击zip进行下载&#xff0c;下载地址为 https://download.java.net/openjdk/jdk8u40/ri/openjdk-8u40-src-b25-10_feb…

centos7 zookeeper3.5.6单点部署

目录 官网下载 部署 连接测试 官网下载 https://zookeeper.apache.org/ 下载 https://zookeeper.apache.org/releases.html 直接部署的包 https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/current/apache-zookeeper-3.5.6-bin.tar.gz 源码包&#xff08;下载这个比较…

PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题

博主欢迎转载&#xff0c;但请给出本文链接&#xff0c;我尊重你&#xff0c;你尊重我&#xff0c;谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 一开始以为是八皇后问题&#…

Pagination(分页) 从前台到后端总结

一&#xff1a;效果图 下面我先上网页前台和管理端的部分分页效果图&#xff0c;他们用的是一套代码。 回到顶部(go to top)二&#xff1a;上代码前的一些知识点 此jQuery插件为Ajax分页插件&#xff0c;一次性加载&#xff0c;故分页切换时无刷新与延迟&#xff0c;如果数据量…

centos7 kafka2.3.1单点部署

依赖环境 kafka依赖zookeeper&#xff0c;故先要进行zookeeper部署&#xff0c;详见centos7 zookeeper部署。 kafka下载 http://kafka.apache.org/downloads http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.3.1/kafka_2.12-2.3.1.tgz 部署 tar xvzf kafka_2.12-2.3.1.t…

zookeeper命令

help帮助命令 ls 查看命令 ls / ls /zookeeper ls /zookeeper/quota /create 创建节点 命令格式&#xff1a;create path data create /whq mytest 创建/whq节点&#xff0c;内容为mytest ///get 查看节点内容 命令格式&#xff1a;get path [zk: localhost:2182(CONNECTED)…

GridControl 选择列、复选框全选(上)

说明&#xff1a; GirdControl 中加入一列&#xff0c;这一列不是写在数据库中的&#xff0c;而是代码中加入的。 图示&#xff1a; 底层类代码&#xff1a; #region GridControl 全选/// <summary>/// 是否选中/// </summary>private static bool chkState false…

msfconsole 无法启动,解决办法

今天突然碰上kali msfconsole 无法启动&#xff0c;经过查找资料&#xff0c;现已成功解决该问题&#xff0c;现将解决办法整理如下&#xff1a; service postgresql start     # 启动数据库服务 msfdb init             # 初始化数据库 msfconsole     …

centos7 zookeeper3.5.6单机伪集群部署

接上篇文章centos7 zookeeper单点部署准备好zookeeper包&#xff0c;进行集群部署 单机伪集群部署 zookeeper1 zookeeper2 zookeeper3 三个目录分别部署一个服务。 cp -r apache-zookeeper-3.5.6-bin/ zookeeper1 cd zookeeper1/ mkdir data vi conf/zoo.cfg 修改 dataDir/op…

centos7 kafka2.3.1单机伪集群部署

接上篇文章centos7 zookeeper单点部署&#xff0c;准备好相应的包 cp config/server.properties config/server0.properties vi config/server0.properties 修改 broker.id0 listenersPLAINTEXT://192.168.81.145:9092 #注意&#xff0c;这里一定要有客户端可访问的ip&…