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. jquery 的 $.extend 和 $.fn.extend

    $.extend({ add:function(a,b){return a+b;}, bad:function(a,b){return a-b;} }); $.fn.extend({ loading: ...

  2. xe7 Unresolved external CSPIN.OBJ

    工程里打开使用了CSPIN控件的界面窗体,再编译就好了 [ilink32 Error] Error: Unresolved external 'Vcl::Controls::TControl::Set ...

  3. JMH使用说明

    JMH使用说明 一.概述 JMH,即Java Microbenchmark Harness,是专门用于代码微基准测试的工具套件.何谓Micro Benchmark呢?简单的来说就是基于方法层面的基准测 ...

  4. 使用.htaccess文件

    禁止对无索引文件的目录进行文件列表展示 默认情况下,当我们访问网站的某个无索引文件(如index.html,index.htm或 index.php)目录时,服务器会显示该目录的文件和子目录列表,这是 ...

  5. dir 使用,统计文件数量

    dir /b /a-d | find /v /c "$$$$" >1.log--※ 来源:·水木社区 newsmth.net·[FROM: 125.46.17.*] 今天去水 ...

  6. python远程调试及celery调试

    部分来自 from: https://www.xncoding.com/2016/05/26/python/pycharm-remote.html 你是否经常要在Windows 7或MAC OS X上 ...

  7. spring boot 测试类

    import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.Logge ...

  8. 【339】matplotlib based on python3

    Ref: python3 的 matplotlib绘图库的使用 Ref: python matplotlib绘图设置坐标轴刻度.文本 Ref: python中matplotlib的颜色及线条控制 Re ...

  9. spring 每个jar的作用

    spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...

  10. spring JPA分页排序条件查询

    @RequestMapping("/listByPage") public Page<Production> listByPage(int page, int size ...