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;
}
}
 

sqlserver实现分页的几种方式的更多相关文章

  1. sqlserver收缩日志的几种方式

    sqlserver收缩日志的几种方式   [sql] --参考    压缩日志及数据库文件大小      /*--特别注意       请按步骤进行,未进行前面的步骤,请不要做后面的步骤    否则可 ...

  2. mysql实现分页的几种方式

    mysql实现分页的几种方式: 第一种:使用框架自带的pageable来进行分页 package com.cellstrain.icell.repository.repositoryImpl; imp ...

  3. springmvc+jpa实现分页的两种方式

    1.工具类 public final class QueryTool { public static PageRequest buildPageRequest(int pageNumber, int ...

  4. 【转载】Sqlserver数据库备份的几种方式

    在实际的数据库Sqlserver的运维的过程中,很多时候我们需要做到数据的备份操作,可以做到定时备份,也可以进行手动数据库备份.在实际的过程中,有时候因业务需要备份出完整数据库,而有时候又因为实际业务 ...

  5. Java项目开发中实现分页的三种方式一篇包会

    前言   Java项目开发中经常要用到分页功能,现在普遍使用SpringBoot进行快速开发,而数据层主要整合SpringDataJPA和MyBatis两种框架,这两种框架都提供了相应的分页工具,使用 ...

  6. (转)SqlServer 数据库同步的两种方式 (发布、订阅),主从数据库之间的同步

    最近在琢磨主从数据库之间的同步,公司正好也需要,在园子里找了一下,看到这篇博文比较详细,比较简单,本人亲自按步骤来过,现在分享给大家. 在这里要提醒大家的是(为了更好的理解,以下是本人自己理解,如有错 ...

  7. SqlServer 数据库同步的两种方式 (发布、订阅),主从数据库之间的同步

    最近在琢磨主从数据库之间的同步,公司正好也需要,在园子里找了一下,看到这篇博文比较详细,比较简单,本人亲自按步骤来过,现在分享给大家. 在这里要提醒大家的是(为了更好的理解,以下是本人自己理解,如有错 ...

  8. 从官方文档中探索MySQL分页的几种方式及分页优化

    概览 相比于Oracle,SQL Server 等数据库,MySQL分页的方式简单得多了,官方自带了分页语法 limit 语句: select * from test_t LIMIT {[offset ...

  9. yii2-搜索带分页,分页的两种方式

    1.文章表关联 <?php //...other code //关联 public function getCate(){ return $this->hasOne(ArticleCate ...

随机推荐

  1. 20.OGNL与ValueStack(VS)-普通方法访问

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 首先在User中增加一个成员方法,代码如下: public String g ...

  2. 客户端如何连接 DataSnap Server 调用服务的方法

    一般http访问的地址是 http://localhost:8099/datasnap/rest/TServerMethods1/EchoString/abc 一.用FDConnection1连接Da ...

  3. insert NULL into mysql

    https://stackoverflow.com/questions/36898130/python-how-to-insert-null-mysql-values You are insertin ...

  4. Spring在代码中获取properties文件属性

    这里介绍两种在代码中获取properties文件属性的方法. 使用@Value注解获取properties文件属性: 1.因为在下面要用到Spring的<util />配置,所以,首先要在 ...

  5. iBatis与Hibernate有什么不同?

    相同点:屏蔽jdbc api的底层访问细节,使用我们不用与jdbc api打交道,就可以访问数据. jdbc api编程流程固定,还将sql语句与java代码混杂在了一起,经常需要拼凑sql语句,细节 ...

  6. Appium客户端,命令行启动server

    目标:通过命令行启动Appium的server   1.通过命令行安装的Appium   直接命令行输入appium即可启动服务   2.安装的Appium客户端   可以查看客户端中打印的启动日志: ...

  7. php71 gdnz

    更新yum库:yum updat yum install epel-release yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel ...

  8. Mysql操作日志

    任何一种数据库中,都有各种各样的日志.MySQL也不例外,在Mysql中有4种不同的日志.分别错误日志.二进制日志.查询日志和慢查询日志.这些日志记录着Mysql数据库不同方面的踪迹.下文将介绍这4种 ...

  9. BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource

    Tomcat报错如下: BeanDefinitionStoreException: IOException parsing XML document from ServletContext resou ...

  10. obstacle

    obstacle - 必应词典 美[ˈɑbstək(ə)l]英[ˈɒbstək(ə)l] n.障碍:障碍物:阻碍:绊脚石 网络妨碍:干扰:妨害