sqlserver实现分页的几种方式

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

  1. package com.cellstrain.icell.repository.repositoryImpl;
  2.  
  3. 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;
  4.  
  5. import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    import java.util.List;
    import java.util.Map;
  6.  
  7. @Repository(value = "vPaperRepository")
    public class VPaperRepositoryImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;
  8.  
  9. @PersistenceContext
    private EntityManager entityManager;
  10.  
  11. /**
    * 后台分页查询
    * @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);
    }
  12.  
  13. }
  14.  
  15. 第二种:使用top关键字来进行分页
  1. package com.cellstrain.icell.repository.repositoryImpl;
  2.  
  3. 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;
  4.  
  5. import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    import java.util.List;
    import java.util.Map;
  6.  
  7. @Repository(value = "vPaperRepository")
    public class VPaperRepositoryImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;
  8.  
  9. @PersistenceContext
    private EntityManager entityManager;
  10.  
  11. /**
    * 后台分页查询
    * @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;
    }
    }
  1.  

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. leetcode447

    public class Solution { /// <summary> /// 计算两个点的距离 /// </summary> /// <param name=&qu ...

  2. android -chrome 调试

    在chrome上 输入 chrome://inspect/ 连接手机,配置 监听8000,和8080端口 cordova默认是8000端口 如果出现白屏:原因:google在首次加载时,要进行服务器连 ...

  3. 前端-CSS-10-定位

    <!-- 定位有三种: 1.相对定位 2.绝对定位 3.固定定位 这三种定位,每种定位都暗藏玄机,所以我们要一一单讲 position:relative; position:absolute; ...

  4. session第二篇

    二 A.application对象 1.application对象实现了用户间数据的共享,可存放全局变量. 2.application对象开始于服务器的启动,终止于服务器的关闭. 3.在用户的前后连接 ...

  5. 脱离SVN的控制

    在桌面创建一个记事本文件,然后吧这句话复制进去for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn ...

  6. Linux就业技术指导(一):简历撰写及面试筹备要领

    一,开场 二,模型 三,目标选材 3.1 什么是目标选材 简单说就是确定一个候选人是否符合某一个工作岗位要求的整个流程.这是对招聘方的一个培训,应聘方如果掌握了,就知道应该怎样正确的去应聘工作. 3. ...

  7. twitter 等网站console.log不能使用,可用alert或者

    function setConsole() { var iframe = document.createElement('iframe'); iframe.style.display = 'none' ...

  8. IDEA artifacts Web Application:Exploded Web Application:Archive

    首先,artifacts是maven中的一个概念,表示项目/modules如何打包,比如jar,war,war exploded,ear等打包形式,一个项目或者说module有了artifacts 就 ...

  9. sqldatareader无法得到output参数的解决

    只需要在所有的sqldatareader结束后,加上一句就可以得到输出参数了. sdr.Close(); Object ObjCount = cmd.Parameters["@Count_P ...

  10. springboot重定向

    参考https://www.cnblogs.com/kxkl123/p/7800967.html public String test() { return "redirect:/" ...